-
Notifications
You must be signed in to change notification settings - Fork 178
Usage Cheatsheet
Robert Coltheart edited this page May 8, 2019
·
6 revisions
[Subject(typeof(Car))]
class When_using_a_car
{
static Car car;
static bool stopped;
Establish context = () =>
car = new Car();
Because of = () =>
stopped = car.StopCar();
It should_turn_off_engine = () =>
stopped.ShouldBeTrue();
}
[Subject(typeof(Car))]
class When_a_rotary_engine_is_used
{
static Car car;
static IEngine engine;
static bool stopped;
// 1. Executed first
Establish context = () =>
engine = new RotaryEngine();
class When_using_a_compact_car
{
// 2. Executed second
Establish context = () =>
car = new CompactCar(engine);
Because of = () =>
stopped = car.StopCar();
It should_turn_off_engine = () =>
stopped.ShouldBeTrue();
}
class When_using_a_sports_car
{
// 2. Executed second
Establish context = () =>
car = new SportsCar(engine);
Because of = () =>
stopped = car.StopCar();
It should_turn_off_engine = () =>
stopped.ShouldBeTrue();
}
}
- Machine.Fakes.Moq
- Machine.Fakes.NSubstitute
- Machine.Fakes.FakeItEasy
- Machine.Fakes.RhinoMocks - no support for .NET Core
class When_using_a_car : WithSubject<Car>
{
Because of = () =>
Subject.StartCar();
}
class When_using_a_car : WithSubject<Car>
{
static bool engine_off;
Establish context = () =>
The<IEngine>()
.WhenToldTo(x => x.TurnOff(Param.IsAny<MethodUsed>()))
.Return(true);
Because of = () =>
engine_off = Subject.StopCar();
It should_turn_off_engine = () =>
engine_off.ShouldBeTrue();
}
Establish context = () =>
The<IEngine>()
.WhenToldTo(x => x.TurnOff(Param.IsAny<MethodUsed>()))
.Throw(new InvalidOperationException());
It should_turn_off_engine_once = () =>
The<IEngine>()
.WasToldTo(x => x.TurnOff(Param.IsAny<MethodUsed>()))
.OnlyOnce();
It should_turn_off_engine_once = () =>
The<IEngine>()
.WasNotToldTo(x => x.TurnOff(Param.IsAny<MethodUsed>()));
public class Car
{
public Car(IEngine engine)
{
}
}
class When_using_a_car : WithSubject<Car>
{
Establish context = () =>
Configure(x => x.For<IEngine>().Use<RotaryEngine>());
}
Only works on void
return methods.
Establish context = () =>
The<IEngine>()
.WhenToldTo(x => x.Service(Param.IsAny<bool>(), Param.IsAny<int>()))
.Callback<bool, int>((x, y) =>
{
is_full_service = x;
tires_count = y;
});
Only works on void
return methods.
Establish context = () =>
{
bool changed;
The<IEngine>()
.WhenToldTo(x => x.ChangeTire(1, out changed))
.AssignOutAndRefParameters(true);
};
In the above changed
will be set to true.
Getting Started
Test Runners
Guidelines