I am very new to TDD, ASP.NET MVC and LinqToSql. I am trying to write tests for my repository, which gets data from stored procedure using LinqToSql.
This is the repository method being tested:
public int GetResponseCount(int campaignId)
{
var results = (new MyDBDataContext()).GetResponseCountById(campaignId);
foreach (GetResponseCountByIdResult result in results)
{
return (result.ResponseCount != null) ? result.ResponseCount.Value : 0;
}
return 0;
}
This is the code in test:
var myDBDataContext = new Mock<MyDBDataContext>();
var result = new Mock<ISingleResult<GetResponseCountByIdResult>>().Object();
myDBDataContext.Setup(x => x.GetResponseCountById(It.IsAny<int>())).Returns(result).Verifiable();
...
...
There are 2 questions:
1) The sored procedure returns a single integer result. But I am having to loop through the response from LinqToSql to get to the value. Which is not ideal. Is there a better way to do it?
2) The code in test does not compile. The compiler says - Non-invocable member 'Moq.Mock<System.Data.Linq.ISingleResult<GetResponseCountByResult>>.Object' cannot be used like a method.
How can I write test to verify that my repository method calls the right method in LinqToSql?
Many Thanks!