and the other thing missing from all of this is your "real" repository -- that will do the actual work to connect to a databas and it will be used at runtime.
Yes I understand that the idea is not to test the database work but rather the correct object calls etc.
I've refactored the code but I still feel like I'm missing someting:
Tests (missed out the SaveIt call before):
[Test]
public void TestMock()
{
Mock<IRepository> mockRepository = new Mock<IRepository>();
var subject = new ThingToTest(mockRepository.Object);
var someObj = new SomeObject();
subject.SaveIt(someObj);
mockRepository.Verify(x => x.Save());
}
Thing to Test:
public class ThingToTest
{
private IRepository _repository;
public ThingToTest(IRepository repository)
{
this._repository = repository;
}
public void SaveIt(SomeObject someObject)
{
someObject.Save();
}
}
Yep, looks good. So then what you'd be testing is logic in ThingToTest.SaveIt. For example two tests that confirm some behavior:
public class ThingToTest
{
private IRepository _repository;
public ThingToTest(IRepository repository)
{
this._repository = repository;
}
public void SaveIt(SomeObject someObject)
{
if (someObject.SomeValueThatShouldBeNonNull != null)
{
_repository.Save(someObject);
}
}
}
[Test]
void WhenSomeValueThatShouldBeNonNullIsNonNull_RepositoryShouldBeCalledToSave()
{
Mock<IRepository> mockRepository =newMock<IRepository>(); var subject =newThingToTest(mockRepository.Object); var someObj =newSomeObject() {SomeValueThatShouldBeNonNull = "this is not null"}; subject.SaveIt(someObj); mockRepository.Verify(x => x.Save());
Ok thanks for the help. The last thing I just want to confirm - The SomeObject object handles code for both mocking and performing the real task (writing to a database), and not just a clone of another object that does the real implementation. Am I correct?
Ok thanks for the help. The last thing I just want to confirm - The SomeObject object handles code for both mocking and performing the real task (writing to a database), and not just a clone of another object that does the real implementation. Am I correct?
SomeObject is mainly just a container for data in and data out. If you noticed I editied my post (had a typo). The SomeObject is the param into the repository, not the object that does the DB work. In the test there will be no real database activity, but
from the ThingToTest's perspective it doesn't know because all of that is hidden behind the IRepository interface. He doesn't know if it's a real or a fake (and that's a nice level of decoupling).
Ah I think it's all starting to click into place. I'm going to have to do some serious work into this though to really understand it properly. Thanks for your help, original post marked as answer
Ah I think it's all starting to click into place. I'm going to have to do some serious work into this though to really understand it properly. Thanks for your help, original post marked as answer
Yea, it takes a while. I'd say it took me a year before I even got comfortable with the style.
BrockAllen
All-Star
27432 Points
4890 Posts
MVP
Re: Database Mocking
Mar 27, 2012 08:51 PM|LINK
heh, yea.. sorry.
and the other thing missing from all of this is your "real" repository -- that will do the actual work to connect to a databas and it will be used at runtime.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Cerberus2589
Member
259 Points
144 Posts
Re: Database Mocking
Mar 27, 2012 08:56 PM|LINK
Yes I understand that the idea is not to test the database work but rather the correct object calls etc.
I've refactored the code but I still feel like I'm missing someting:
Tests (missed out the SaveIt call before):
[Test] public void TestMock() { Mock<IRepository> mockRepository = new Mock<IRepository>(); var subject = new ThingToTest(mockRepository.Object); var someObj = new SomeObject(); subject.SaveIt(someObj); mockRepository.Verify(x => x.Save()); }Thing to Test:
public class ThingToTest { private IRepository _repository; public ThingToTest(IRepository repository) { this._repository = repository; } public void SaveIt(SomeObject someObject) { someObject.Save(); } }BrockAllen
All-Star
27432 Points
4890 Posts
MVP
Re: Database Mocking
Mar 27, 2012 09:02 PM|LINK
Edit: Sorry for the crappy formatting.
Yep, looks good. So then what you'd be testing is logic in ThingToTest.SaveIt. For example two tests that confirm some behavior:
public class ThingToTest { private IRepository _repository; public ThingToTest(IRepository repository) { this._repository = repository; } public void SaveIt(SomeObject someObject) { if (someObject.SomeValueThatShouldBeNonNull != null) { _repository.Save(someObject); } } } [Test] void WhenSomeValueThatShouldBeNonNullIsNonNull_RepositoryShouldBeCalledToSave() {}
[Test]
void WhenSomeValueThatShouldBeNonNullIsNull_RepositoryShouldNotBeCalledToSave()
{
}DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Cerberus2589
Member
259 Points
144 Posts
Re: Database Mocking
Mar 27, 2012 09:07 PM|LINK
Ok thanks for the help. The last thing I just want to confirm - The SomeObject object handles code for both mocking and performing the real task (writing to a database), and not just a clone of another object that does the real implementation. Am I correct?
BrockAllen
All-Star
27432 Points
4890 Posts
MVP
Re: Database Mocking
Mar 27, 2012 09:17 PM|LINK
SomeObject is mainly just a container for data in and data out. If you noticed I editied my post (had a typo). The SomeObject is the param into the repository, not the object that does the DB work. In the test there will be no real database activity, but from the ThingToTest's perspective it doesn't know because all of that is hidden behind the IRepository interface. He doesn't know if it's a real or a fake (and that's a nice level of decoupling).
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Cerberus2589
Member
259 Points
144 Posts
Re: Database Mocking
Mar 27, 2012 09:28 PM|LINK
Ah I think it's all starting to click into place. I'm going to have to do some serious work into this though to really understand it properly. Thanks for your help, original post marked as answer
BrockAllen
All-Star
27432 Points
4890 Posts
MVP
Re: Database Mocking
Mar 27, 2012 09:33 PM|LINK
Yea, it takes a while. I'd say it took me a year before I even got comfortable with the style.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/