Estoy usando xUnit, SubSpec y FakeItEasy para las pruebas de mi unidad. que he creado hasta ahora algunas pruebas unitarias positivas como las siguientes:Cómo probar las excepciones lanzadas con xUnit, SubSpec y FakeItEasy
"Given a Options presenter"
.Context(() =>
presenter = new OptionsPresenter(view,
A<IOptionsModel>.Ignored,
service));
"with the Initialize method called to retrieve the option values"
.Do(() =>
presenter.Initialize());
"expect the view not to be null"
.Observation(() =>
Assert.NotNull(view));
"expect the view AutoSave property to be true"
.Observation(() => Assert.True(view.AutoSave));
Pero ahora quiero escribir algunas pruebas unitarios negativos y comprobar que ciertos métodos no se les llama, y se produce una excepción
por ej.
"Given a Options presenter"
.Context(() =>
presenter = new OptionsPresenter(view,
A<IOptionsModel>.Ignored,
service));
"with the Save method called to save the option values"
.Do(() =>
presenter.Save());
"expect an ValidationException to be thrown"
.Observation(() =>
// TODO
);
"expect an service.SaveOptions method not to be called"
.Observation(() =>
// TODO
);
puedo ver FakeItEasy tiene un método de extensión MustNotHaveHappened, y xUnit tiene un método Assert.Throws.
¿Pero cómo lo pongo todo junto?
La excepción que quiero probar debe producirse cuando se llama al método Guardar. Así que supongo que debería envolver un método de Assert.Throws alrededor del presentador. Llamada al método Save(), pero pensé que el método presentador.Save debería llamarse en .Do (() => ...
¿puede usted por favor avise si mi prueba de la unidad debe ser similar por debajo o por alguna otra cosa?
"Given a Options presenter"
.Context(() =>
presenter = new OptionsPresenter(view,
model,
service));
"expect the Presenter.Save call to throw an Exception"
.Observation(() =>
Assert.Throws<FluentValidation.ValidationException>(() => presenter.Save()));
"expect the Service.SaveOptions method not to be called"
.Observation(() =>
A.CallTo(() => service.SaveOptions(A<IOptionsModel>.Ignored)).MustNotHaveHappened());
Muchas gracias
No estoy seguro de que esto podría ayudar, pero no se compruebe la documentación sobre SubSpec por ejemplo https://bitbucket.org/johannesrudolph/subspec/src/a35fcc8ae1f6/test/SubSpec.Tests/ContextSetupTeardownBehavior.cs también estos son Pruebas basadas en BDD/Especificación no Pruebas unitarias. Es posible que tenga una mejor audiencia si incluye la etiqueta BDD. – Spock