This is a migrated thread and some comments may be shown as answers.

Mocking a Repository

6 Answers 144 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
johnv
Top achievements
Rank 2
johnv asked on 24 Apr 2010, 10:15 PM
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


6 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 28 Apr 2010, 02:10 PM
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.
0
johnv
Top achievements
Rank 2
answered on 28 Apr 2010, 04:11 PM
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!


0
Chris
Telerik team
answered on 29 Apr 2010, 02:30 PM
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.
0
James
Top achievements
Rank 1
answered on 10 Jun 2011, 01:56 PM
0
Bob
Top achievements
Rank 1
answered on 26 Aug 2011, 08:15 AM
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

0
Ricky
Telerik team
answered on 26 Aug 2011, 11:31 AM
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 >>

Tags
General Discussions
Asked by
johnv
Top achievements
Rank 2
Answers by
Ricky
Telerik team
johnv
Top achievements
Rank 2
Chris
Telerik team
James
Top achievements
Rank 1
Bob
Top achievements
Rank 1
Share this question
or