Kman
Kman

Reputation: 4991

NSubstitute test for void method

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).
}

Upvotes: 0

Views: 828

Answers (1)

mu88
mu88

Reputation: 5374

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.

Upvotes: 1

Related Questions