I am writing Unit/Component test using In Memory DB. When I write Unit/Component test, I came across with the following question.
I have the following two BL methods.
- ToCreate
- ToGet
So when I write Unit/Component test, I need to create some sample data for the unit test.
When I write a Unit/Component test for "ToGet" method, can I use ToCreate (BL method) to create sample data or When I write a Unit/Component test for "ToCreate", Can I use "ToGet" method to check the created data? Is that a correct choice?
-
1Are these two methods tightly related? I mean would you consider these two to work along as a unit? Do they both solve in collaboration a specific domain "problem"? Does toCreate depend on toGet or vice-versa? On the other hand. What are you really testing? The methods? The persistence?Laiv– Laiv2019年02月18日 07:50:55 +00:00Commented Feb 18, 2019 at 7:50
-
The two methods are independent and ]I want to test the methods, not the persistence. But when I look an example for that I found In Memory provider gives an easy implementation. youtube.com/watch?v=ddrR440JtiAJeeva Jsb– Jeeva Jsb2019年02月18日 09:08:08 +00:00Commented Feb 18, 2019 at 9:08
-
If you want to test the methods without persistence, it means that none of them will have access to the in-memory DB. In that case, how do you expect "toGet" validate what "toCreate" has created (or vice-versa)?Laiv– Laiv2019年02月18日 09:54:04 +00:00Commented Feb 18, 2019 at 9:54
-
So per your suggestion, I should go only with mock-up data if I am testing only methods. So In case If I write a unit test to test persistence too with the method, is that ok to use "toCreate" method to create test data while writing the unit test for "toGet" method.Jeeva Jsb– Jeeva Jsb2019年02月18日 10:13:45 +00:00Commented Feb 18, 2019 at 10:13
1 Answer 1
Perhaps you should consider writing unit tests from a more coarse grained perspective. Try focusing on usage scenarios and features instead of methods of a class.
For instance, you could have a unit test like
[Fact]
public void PersistedObjectCanBeFetched() {
...
}
instead of two tests like
[Fact]
public void CreateSucceeds() {
...
}
[Fact]
public void GetSucceeds() {
...
}
A single test might call many methods of the unit under test if that's what the scenario requires. What's important is that features of the tested unit work as a whole.