Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created October 20, 2023 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trikitrok/accd8ae8a59f6e730e00335eb19f30c3 to your computer and use it in GitHub Desktop.
Save trikitrok/accd8ae8a59f6e730e00335eb19f30c3 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using NSubstitute;
using NSubstitute.Exceptions;
using Xunit;namespace KataTirePressureVariation.Test
{
public class AlarmShould
{
private Sensor sensor;
Notifier notifier;
[Fact]
public void alarm_activates_because_pressure_is_too_low()
{
var alarm = CreateAlarm(16);
alarm.Check();
notifier.Received().Notify("Alarm activated!");
}
[Fact]
public void alarm_activates_because_pressure_is_too_high()
{
var alarm = CreateAlarm(22);
alarm.Check();
notifier.Received().Notify("Alarm activated!");
}
[Theory]
[InlineData(17)]
[InlineData(21)]
public void activated_alarm_deactivates_because_pressure_is_safe(int pressure)
{
var alarm = CreateActivatedAlarm(pressure);
alarm.Check();
Received.InOrder(() => {
notifier.Received().Notify("Alarm activated!");
notifier.Received().Notify("Alarm deactivated!");
});
}
[Fact]
public void deactivated_alarm_after_being_active_activates_because_pressure_is_not_safe()
{
var alarm = CreateDeactivatedAlarmAfterBeingActive(50);
alarm.Check();
Received.InOrder(() => {
notifier.Received().Notify("Alarm activated!");
notifier.Received().Notify("Alarm deactivated!");
notifier.Received().Notify("Alarm activated!");
});
}
private Alarm CreateAlarm(int pressure)
{
sensor = Substitute.For<Sensor>();
sensor.PopNextValue().Returns(pressure);
notifier = Substitute.For<Notifier>();
return new Alarm(sensor, notifier);
}
private Alarm CreateActivatedAlarm(int pressure)
{
sensor = Substitute.For<Sensor>();
sensor.PopNextValue().Returns(15, pressure);
notifier = Substitute.For<Notifier>();
var alarm = new Alarm(sensor, notifier);
alarm.Check();
return alarm;
}
private Alarm CreateDeactivatedAlarmAfterBeingActive(int pressure)
{
sensor = Substitute.For<Sensor>();
sensor.PopNextValue().Returns(15, 18, pressure);
notifier = Substitute.For<Notifier>();
var alarm = new Alarm(sensor, notifier);
alarm.Check();
alarm.Check();
return alarm;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment