如何模拟 Subsonic ExecuteReader 方法?

发布于 2024-11-13 23:01:50 字数 1590 浏览 6 评论 0原文

我有一个调用存储过程并在执行 DataReader 后返回数据的方法。 我正在尝试使用模拟测试该方法。我不知道如何返回值?

有人做过这个吗?感谢您的回复。

这是我的代码:

// Call the StoredProcedure
    public List<string> GetCompletedBatchList(int fileId)
    {
        List<string> completedBatches = new List<string>();

        StoredProcedure sp = new StoredProcedure("GetDistributedBatches", this.dataProvider);
        sp.Command.AddParameter("FileID", fileId, DbType.Int32, ParameterDirection.Input);
        sp.Command.AddParameter("Result", null, DbType.Int32, ParameterDirection.InputOutput);
        using (var rdr = sp.ExecuteReader())
        {
            while (rdr != null && rdr.Read())
            {
                if (rdr[0] != null)
                {
                    completedBatches.Add(rdr[0].ToString());
                }
            }
        }
        return completedBatches;
    }

这是测试方法:

[Test]
    public void Can_get_completedBatches()
    {
        var file = new File() { FileID = 1, DepositDate = DateTime.Now };
        repo.Add<File>(file);

        CompletedBatches completedBatches = new CompletedBatches(provider.Object);


        //Here I am not sure how to Return  
        provider.Setup(**x => x.ExecuteReader(It.IsAny<QueryCommand>())).Returns**  =>
        {
            cmd.OutputValues.Add(0);
        });
        var completedBatchesList = completedBatches.GetCompletedBatchList(file.FileID);
        Assert.AreEqual(0, completedBatchesList.Count());

    }

I have a method that calls stored procedure and returns the data after executing DataReader.
I am trying to test the method using mock. I am not sure how to return value?

Anyone did this? Appreciate your responses.

Here is my code:

// Call the StoredProcedure
    public List<string> GetCompletedBatchList(int fileId)
    {
        List<string> completedBatches = new List<string>();

        StoredProcedure sp = new StoredProcedure("GetDistributedBatches", this.dataProvider);
        sp.Command.AddParameter("FileID", fileId, DbType.Int32, ParameterDirection.Input);
        sp.Command.AddParameter("Result", null, DbType.Int32, ParameterDirection.InputOutput);
        using (var rdr = sp.ExecuteReader())
        {
            while (rdr != null && rdr.Read())
            {
                if (rdr[0] != null)
                {
                    completedBatches.Add(rdr[0].ToString());
                }
            }
        }
        return completedBatches;
    }

Here is the Test Method:

[Test]
    public void Can_get_completedBatches()
    {
        var file = new File() { FileID = 1, DepositDate = DateTime.Now };
        repo.Add<File>(file);

        CompletedBatches completedBatches = new CompletedBatches(provider.Object);


        //Here I am not sure how to Return  
        provider.Setup(**x => x.ExecuteReader(It.IsAny<QueryCommand>())).Returns**  =>
        {
            cmd.OutputValues.Add(0);
        });
        var completedBatchesList = completedBatches.GetCompletedBatchList(file.FileID);
        Assert.AreEqual(0, completedBatchesList.Count());

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-11-20 23:01:50

如果您想创建特定形状和大小的 DataReader,那么我建议您查看 DataTable.CreateDataReader DataTable.CreateDataReader。然后,您可以在示例中设置 ExecuteReader 以返回此数据读取器。

If you want to create a DataReader of a certain shape and size then I suggest you look at DataTable.CreateDataReader DataTable.CreateDataReader. You can then setup the ExecuteReader in your example to return this datareader.

清旖 2024-11-20 23:01:50

以下链接帮助我...
如何使用 Moq - Update 模拟 SqlDataReader

我使用了 MockDbDataReader模拟数据的方法

 [Test]
        public void Can_get_completedBatches_return_single_batch()
        {
            var date = DateTime.Now;
            var file = new File() { FileID = 202, DepositDate = DateTime.Now };
            var batch1 = new Batch() { FileID = 202, BatchID = 1767, LockboxNumber = "1", IsLocked = true, LockedBy = "testUser" };
            var transaction1 = new Transaction() { BatchID = 1767, TransactionID = 63423, CheckAmount = 100.0 };
            var distribution1 = new Distribution() { TransactionID = 63423, InvoiceNumber = "001", Amount = 100.0, DateCreated = date, DateModified = date, TransType = 2 };

            repo.Add<File>(file);
            repo.Add<Batch>(batch1);
            repo.Add<Transaction>(transaction1);
            repo.Add<Distribution>(distribution1);

            CompletedBatches completedBatches = new CompletedBatches(provider.Object);

            provider.Setup(x => x.ExecuteReader(It.IsAny<QueryCommand>())).Returns(MockDbDataReader());

            var completedBatchesList = completedBatches.GetCompletedBatchList(202);
            Assert.AreEqual(1, completedBatchesList.Count());

        }


// You should pass here a list of test items, their data
    // will be returned by IDataReader
    private DbDataReader MockDbDataReader(List<TestData> ojectsToEmulate)
    {
        var moq = new Mock<DbDataReader>();

        // This var stores current position in 'ojectsToEmulate' list
        int count = -1;

        moq.Setup(x => x.Read())
            // Return 'True' while list still has an item
            .Returns(() => count < ojectsToEmulate.Count - 1)
            // Go to next position
            .Callback(() => count++);

        moq.Setup(x => x["BatchID"])
            // Again, use lazy initialization via lambda expression
            .Returns(() => ojectsToEmulate[count].ValidChar);

        return moq.Object;
    }

The following link helped me...
How to mock an SqlDataReader using Moq - Update

I used MockDbDataReader method to mock the data

 [Test]
        public void Can_get_completedBatches_return_single_batch()
        {
            var date = DateTime.Now;
            var file = new File() { FileID = 202, DepositDate = DateTime.Now };
            var batch1 = new Batch() { FileID = 202, BatchID = 1767, LockboxNumber = "1", IsLocked = true, LockedBy = "testUser" };
            var transaction1 = new Transaction() { BatchID = 1767, TransactionID = 63423, CheckAmount = 100.0 };
            var distribution1 = new Distribution() { TransactionID = 63423, InvoiceNumber = "001", Amount = 100.0, DateCreated = date, DateModified = date, TransType = 2 };

            repo.Add<File>(file);
            repo.Add<Batch>(batch1);
            repo.Add<Transaction>(transaction1);
            repo.Add<Distribution>(distribution1);

            CompletedBatches completedBatches = new CompletedBatches(provider.Object);

            provider.Setup(x => x.ExecuteReader(It.IsAny<QueryCommand>())).Returns(MockDbDataReader());

            var completedBatchesList = completedBatches.GetCompletedBatchList(202);
            Assert.AreEqual(1, completedBatchesList.Count());

        }


// You should pass here a list of test items, their data
    // will be returned by IDataReader
    private DbDataReader MockDbDataReader(List<TestData> ojectsToEmulate)
    {
        var moq = new Mock<DbDataReader>();

        // This var stores current position in 'ojectsToEmulate' list
        int count = -1;

        moq.Setup(x => x.Read())
            // Return 'True' while list still has an item
            .Returns(() => count < ojectsToEmulate.Count - 1)
            // Go to next position
            .Callback(() => count++);

        moq.Setup(x => x["BatchID"])
            // Again, use lazy initialization via lambda expression
            .Returns(() => ojectsToEmulate[count].ValidChar);

        return moq.Object;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文