0

I would like to verify that the parameter idStatus is assigned to the myObj instance. How can I do this with NSubstitute? I thought that if I somehow could verify the parameter to UpdateObject this would be a good test of the method, but not sure how to do this. Any thoughts?

public void SetObjectStatus(int id, int? idStatus)
{
    var myObj = GetObject(id);
    myObj.IdStatus = idStatus;

    UpdateObject(myObj);
}


[TestMethod]
public void SetObjectStatus_Verify()
{
    //Arrange
    int id = 1;
    int? newStatus = 10;

    //Act
    test.SetObjectStatus(id, newStatus);

    //Assert
    //?? I would like to check that myObj.IdStatus equals newStatus (10).
}
Kman
  • 4,809
  • 7
  • 38
  • 62

1 Answers1

1

It depends whether you want to test the internals (white box testing) or just the result (black box testing).

For the first: does your code give you the flexibility that GetObject(id) can be set up so that it actually returns a mock? If so, you can set up a mock with var mock = Substitute.For<IYourObject>() and call mock.Received().IdStatus (see here).

For the second: you need to call GetObject(id) within your test method and see whether the expected value is present.

mu88
  • 4,156
  • 1
  • 23
  • 47
  • It's all interfaces so flexibility is no problem. One question though. It seems that it's not possible to do a Received() for a method in the class currently under test. Is there a way around? – Kman Apr 02 '20 at 09:55
  • Is it possible for you to provide the complete code? Because when using `Received()`, you have to inject the mock somehow. How are you doing that? As far as I understand the docs, it should work that way... – mu88 Apr 02 '20 at 11:20