How can I handle ISingleResult? Do I need to? or is there a better method?

In my controller there is such a way of action

public ActionResult Index() { using (NorthwindDataContext db = new NorthwindDatacontext()) { var results = db.GetRecordSets(arg1, ....).ToList(); // use results as list } return View(); } 

and I wanted to start doing tests for him (yes, after it was built, not before ... but the code was written before I started using TDD like that ...)

and I realized that adding a property like this to the controller

 public delegate NorthwindDatacontext ContextBuilderDelegate(); public ContextBuilderDelegate ContextBuilder { get; set; } 

I could add something like this to the constructor ...

 ContextBuilder = () => new NorthwindDatacontext(); 

then I can verify that ActionMethod sets the ContextBuilder property with the NorthwindDatacontext layout

 var controller = new MyController(); var mockDataContext = new Mock<NorthwindDatacontext>(); controller.ContextBuilder = () => mockDataContext.Object; 

But ... I did not find a way to use this, because all NorthwindDatacontext methods use ISingleResult as returnType, and I cannot find a way to create an object with this interface. I tried this

 var theResult = new List<GetRecordSetsResult>(); // fill the data structure here with the provided result... mockDataContext.Setup(c => c. GetRecordSets()).Returns(theResult as ISingleResult<GetRecordSetsResult>); 

but this will not work, because when converting to ISingleResultResult it is null.

Is there a way to create an ISingleResult object to test this method, or am I doing the wrong way to do something here?

Thanks at Advance

+6
asp.net-mvc moq
source share
2 answers

I created a class that implemented ISingleResult and just put a list in it. I am new to this type of coding, so for now it worked for me, use at your own risk (and if you see that the holes leave a comment).

 class SingleResult<T>:ISingleResult<T> { readonly List<T> _list = new List<T>(); public void Add(T item) { _list.Add(item); } #region Interface Items public IEnumerator<T> GetEnumerator() { return _list.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public object ReturnValue { get { return _list; } } public void Dispose() { } #endregion } 

This can then be used to return to parts of the layout. This is how I used it with Rhino Mocks:

 [TestMethod] public void TestSomething() { //Arrange // Make a data context and DAL var _ctx = MockRepository.GenerateMock<IDataClassesDataContext>(); var someDALClass = new SomeDALClass(_ctx); User testUser = UserObjectHelper.TestUser(); SingleResult<User> userList = new SingleResult<User> { testUser }; // Indicate that we expect a call the to sproc GetUserByUserID _ctx.Expect(x => x.GetUserByUserID(testUser.UserID)).Return(userList); //Act someDALClass.UpdateUser(testUser); //Assert Assert.IsTrue(SomeTestCondition()); } 
+5
source share

ToList() is an extension method for IEnumerable that is easily mocked because it has only one member method - GetEnumerator() .

However, you may have problems mocking the NorthwindDataContext class if its methods are not virtual ...

In any case, as I solved a similar problem in my sandbox, I hope this helps:

 public class MyType { public virtual ISingleResult<int> ReturnSomeResult() { throw new NotImplementedException(); } } [TestMethod] public void TestMethod1() { var mockMyType = new Mock<MyType>(); var mockSingleResult = new Mock<ISingleResult<int>>(); IEnumerable<int> someEnumerable = new int[] {1,2,3,4,5}; mockSingleResult.Setup(result => result.GetEnumerator()).Returns(someEnumerable.GetEnumerator()); mockMyType.Setup(myType => myType.ReturnSomeResult()).Returns(mockSingleResult.Object); Assert.AreEqual(15, mockMyType.Object.ReturnSomeResult().ToList().Sum()); } 
+3
source share

All Articles