Search code examples
c#unit-testingnsubstituteisolation-frameworks

Am I creating fakes correctly with nsubstitue?


I got an assignment to learn how you use an isolation framework. And I was wondering if I am creating fakes the appropriative way with nsubstitute. Here is a sequence diagram of how the application shoud look like Here.

I have then made unit tests and handed in the exercise. I was told that I didn't use an isolation framework to create fakes(instead of earlier were we made them ourself). Here is an example of how 2 of my tests looked like :

    [Test]
    public void RequestEntry_WithValidId_DoorStateIsClosed()
    {
        var door = new Door();
        var alarm = new Alarm();
        var userValidation = new UserValidation();
        var entryNotifier = Substitute.For<IEntryNotification>();
        var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
        door.DoorCtrl = uut;
        uut.RequestEntry(1);
        Assert.That(uut.DState == DoorState.Closed);
        entryNotifier.Received(1).EntryNotificationGranted();
    }

and

        public void RequestEntry_WithValidId_DoorOpens()
    {
        var door = Substitute.For<IDoor>();
        var alarm = new Alarm();
        var userValidation = new UserValidation();
        var entryNotifier = new EntryNotification();
        var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
        door.DoorCtrl = uut;
        uut.RequestEntry(1);
        door.Received(1).Open();
    }

So I know a few things were wrong in both of these tests as I needed stubs in both of them so I have changed them a bit so they know looks like this :

        public void RequestEntry_WithValidId_CorrectIdParsedToUserValidation()
    {
        var door = Substitute.For<IDoor>();
        var alarm = new Alarm();
        var userValidation = Substitute.For<IUserValidation>();
        userValidation.ValidateEntryRequest(Arg.Any<int>()).Returns(true);
        var entryNotifier = Substitute.For<IEntryNotification>();
        var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
        door.DoorCtrl = uut;
        uut.RequestEntry(1);
        userValidation.Received(1).ValidateEntryRequest(1);
    }

and

        public void RequestEntry_WithValidId_DoorOpens()
    {
        var door = Substitute.For<IDoor>();
        var alarm = new Alarm();
        var userValidation = Substitute.For<IUserValidation>();
        userValidation.ValidateEntryRequest(Arg.Any<int>()).Returns(true);
        var entryNotifier = Substitute.For<IEntryNotification>();
        var uut = new DoorControl(userValidation, entryNotifier, door, alarm);
        door.DoorCtrl = uut;
        uut.RequestEntry(1);
        door.Received(1).Open();
    }

Am I using nsubstitute correctly ? or do I need to add some other things too(The Alarm class isn't used in these two test cases but is required by parameters of the DoorControl constructor, so I could not see a reason to stub them)?


Solution

  • Yes you are using NSubstitute correctly. I can't possibly know if you should have tested something else than what you did.