0

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.

  1. ToCreate
  2. 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?

Jeeva Jsb
  • 101
  • 4
  • 1
    Are 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 Feb 18 '19 at 07: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. https://www.youtube.com/watch?v=ddrR440JtiA – Jeeva Jsb Feb 18 '19 at 09: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 Feb 18 '19 at 09: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 Feb 18 '19 at 10:13

1 Answers1

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.

COME FROM
  • 2,666
  • 15
  • 15