MSpec has a lot of advantages:
37 public class When_setting_the_state_of_a_host
39 private static HostCheckResultCommand command;
40 private static HostCheckResultCommandHandler commandHandler;
41 private static IRepository _repository;
42 private static Host _host;
44 Establish context = () =>
46 _host = new Host("asterix");
47 _repository = MockRepository.GenerateMock<IRepository>();
48 _repository.Expect(x => x.GetById<Host>(""))
51 _repository.Expect(x => x.Add<Host>(null)).IgnoreArguments();
52 command = new HostCheckResultCommand("asterix", HostState.Up);
53 commandHandler = new HostCheckResultCommandHandler(_repository);
56 Because of = () => commandHandler.Execute(command);
57 It should_have_a_HostStateChanged_event = () => ((IEventProvider)_host).GetChanges().First().ShouldBeOfType<HostStateChanged>();
58 It the_event_should_have_HostState_Up = () => ((IEventProvider)_host).GetChanges().First<HostStateChanged>().NewState.ShouldEqual(HostState.Up);
- Once you are used to lambda expresions, it is relative easy to read.
- Since every test is in it's own class, tests can't conflict with each other.
- Every test statement (It) is named and run even when another test statement failes.
- Everything in the class should be static, since the use of anonymous functions.
- Can be difficult to get started.
Well we don't compare the tests, because they are completly different, but we can compare the frameworks.
1 public class A_Person : XPODatabaseTestFixture
4 public void can_be_created()
6 Person person = ObjectSpace.CreateObject<Person>();
The main advantages xUnit.Net has are:
- Every test is performed isolated, for every test method a new instance of the object is created.
- The test runner is very fast, tests are run in different threads, multiple at a time.
- Can use the constructor for common setup and IDisposable for teardown. (Setup and teardown contain logic that is run before and after each test. other frameworks use the [Setup] and [Teardown] attributes)
Although I like MSpec very much, I am going to dump it for xUnit.net. With xUnit.net I don't have to think about making my members static.