Trust me, I've read many articles and questions here on SO. But I haven't found a satisfactory answer. Take Matt Robert's Tutorial on the repository pattern and unit testing as an example:
I don't get it.. do both repositories contain the exact same query logic? It appears to be so, and if so how is that a useful pattern? It would mean whenever I change the query in the fake repository, I have to change the query logic in the actual repository - that sounds like a PITA and recipe for problems.
I would like to keep the query in one method and test that method. It later occurred to me, I could do like this:
//Student can be from the db or a fake list
public person GetStudent(IEnumerable<Student> students,int studentid)
{
return students.FirstOrDefault(s =>s.PersonId ==studentid);
}
//unit test
Assert.IsNull(GetStudent(fakeStudentList, -1))
//actual code
var student = GetStudent(entities.students,-1)
Is there a pattern that mimics above? If so, what is it? Or is that in fact how the repository pattern works? If so, why does it feel like the repository pattern duplicates code?
And also, the effort.. look at this terrifying example :S?
There's not much to go on, but I think you have a flaw in your repository pattern.
You should not pass in a student collection to GetStudent(). GetStudent should be part of a class that internally knows about the student list - whether it's a file, List, database, whatever.
So your "fake" repository may have the same logic, or it may just return a hard-coded dummy Student. If you're faking the repository I assume it because you're testing something else that needs the repository, so it shouldn't care how it got the Student.
Also the article you reference only mentions one repository, so I'm not sure why you're needing duplicating it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With