控制反转-如何模拟亚音速ExecuteReader方法



我有一个方法,它调用存储过程并在执行DataReader后返回数据。我正试着用mock来测试这个方法。我不知道如何返回价值?

谁干的?感谢您的回复。

这是我的代码:

// 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());
    }

如果你想创建一个特定形状和大小的DataReader,我建议你看看DataTable.CreateDataReader DataTable.CCreateDataReader。然后你可以在你的例子中设置ExecuteReader来返回这个数据读取器。

以下链接帮助了我。。。如何使用Moq模拟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;
    }

相关内容

  • 没有找到相关文章

最新更新