Mocking a Repository

7 posts, 1 answers
  1. johnv
    johnv avatar
    36 posts
    Member since:
    Apr 2008

    Posted 24 Apr 2010 Link to this post

    Here is a sample I am working with. My repository works with a LINQ data context against SQL Server 2008. I  would like to assert that my repository works without actually adding records to the database. I am not sure I am doing the correctly. I would like to write tests for my Add, Edit, Delete, and List functions. This is about as simple as it gets for repo's. Can you give me a hand figuring this out?

    I have interfaces and classes for my problem domain; and interfaces and classes for my repositories. What must I do to mock my repositories for testing?

        [TestFixture] 
        class TextHTMLRepositoryTests 
        { 
            TextHTMLRepository _repo; 
     
            [TestFixtureSetUp] 
            public void TestSetup() 
            { 
                _repo = Mock.Create<TextHTMLRepository>(); 
            } 
     
            [Test] 
            public void CreateMockRepo() 
            { 
                Assert.IsNotNull(_repo); 
            } 
     
            [Test] 
            public void NullListTest() 
            { 
                var _texts = _repo.List(); 
                Assert.IsNull(_texts); 
            } 
     
            [Test] 
            public void PostTest() // This test fails 
            { 
                TextHTML _text = Mock.Create<TextHTML>(); 
                _text.TextID = 1; 
                Mock.Assert(() => _repo.Post(_text)); 
            } 
     
            [TestFixtureTearDown] 
            public void TestTearDown() 
            { 
                // TODO: Tear down loose objects 
            } 
        } 

    Thanks,

    John


  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 28 Apr 2010 Link to this post

    Hi johnv,

    The best way to do it is to set your expecatation on the repository itself that is inovked  by LINQ querieis. For example i am doing a save operation on northwind data context and i want it to happen  without ever the cal going to its underlying database:

    var saved = false
    var mockDataContext = Mock.Create<NorthwindDataContextDataContext>(); 
      
    Mock.Arrange(() => mockDataContext.SubmitChanges()).DoInstead(() => 
    saved = true
    }); 
      
    var product = new Product() 
    ProductID = 1, 
    ProductName = "test product"
    UnitsInStock = 3, 
    QuantityPerUnit = "1"
    Discontinued = false
    voa_class = 1 
    }; 
      
    ProductRepository repository = new ProductRepository(mockDataContext); 
      
    repository.Add(product); 
    repository.Save(); 
      
    Assert.IsTrue(mockDataContext.GetChangeSet().Inserts.Count == 1); 
    Assert.IsTrue(saved);


    Here,  optionally i am asserting the insert count that is set to the number of items  that is set during SubmitChanges() call.

    This is kind of basic start, but you can use this same concept for getting data and and performing various operation on top of it.

    Regards,
    Mehfuz

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft R3 2016 release webinar banner
  4. johnv
    johnv avatar
    36 posts
    Member since:
    Apr 2008

    Posted 28 Apr 2010 Link to this post

    This is really very helpful. I definitely recommend that you add this bit to your documentation.

    Thanks for the crash course. It is also nice to know that I don't have to worry about using an interface to abstract the data context. Based on the currently available documentation, I thought that I might have to write an interface.

    Thanks again!


  5. Chris
    Admin
    Chris avatar
    221 posts

    Posted 29 Apr 2010 Link to this post

    Hello johnv,
    Should you have some other questions or problems, please don't hesitate to contact us.

    Best wishes,
    Chris
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Bob
    Bob avatar
    2 posts
    Member since:
    Jul 2010

    Posted 26 Aug 2011 Link to this post

    Started playing around with JustMock and I'm still trying to wrap my head around TDD.

    In Mehfuz's sample, he created a mock of the NorthwindDataContextContext and arranged for the SubmitOnChanges method to set saved=true.

    In my test case, I have simple DataContext called TestDataContext with three tables. When I mock TestDataContext, all three tables are intiialized to null.

    How can I arrange TestDataContext.Table1.InsertOnSubmit to DoNothing()? Table1 is null, so the Mock.Arrange() doesn't do anything.

    I've tried to manually initialize TestDataContext.Table1, but with no luck.

    Any help would be appreciated! Thanks in advance!

    Bob

  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 26 Aug 2011 Link to this post

    Hi Bob,

    Thanks again for reporting the problem. In this regard, i would like to ask what specific test you would like to do. Any test case or sample will be great in order to reproduce it.

    However, during Mock.Arrange if TestContext.Table1 is null it should be able to create the instance automatically so that you can set expectation on TableContext.Table1.InsertOnSubmit() . In that case your profiler should be configured properly. You can further check if your profiler is running as expected through this following line:

    Assert.IsTrue(Mock.IsProfilerEnabled);


    Kind Regards,
    Mehfuz
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Back to Top
DevCraft R3 2016 release webinar banner