1

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

    }
Rita
  • 1,237
  • 5
  • 24
  • 46

2 Answers2

0

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.

Ciaran
  • 541
  • 2
  • 11
0

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;
    }
Community
  • 1
  • 1
Rita
  • 1,237
  • 5
  • 24
  • 46