如何测试使用xUnit,SubSpec和FakeItEasy引发的异常 [英] How to test for exceptions thrown using xUnit, SubSpec and FakeItEasy

查看:116
本文介绍了如何测试使用xUnit,SubSpec和FakeItEasy引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xUnit,SubSpec和FakeItEasy进行单元测试. 到目前为止,我已经创建了一些积极的单元测试,例如:

I’m using xUnit, SubSpec and FakeItEasy for my unit tests. I’ve so far created some positive unit tests like the following:

"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));

但是现在我想编写一些否定的单元测试,并检查某些方法没有被调用,并且引发了异常

But now I want to write some negative unit tests and check that certain methods don't get called, and an exception is thrown

例如

"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 
     );

我可以看到FakeItEasy有一个MustNotHaveHappened扩展方法,而xUnit有一个Assert.Throws方法.

I can see FakeItEasy has a MustNotHaveHappened extension method, and xUnit has an Assert.Throws method.

但是我怎么把它们放在一起呢?

But how do I put it all together?

我要测试的异常应该在调用Save方法时发生.所以我想我应该在presenter.Save()方法调用周围包装一个Assert.Throws方法,但是我认为presenter.Save方法应该在.Do(()=> ...

The exception I want to test for should occur when the Save method is called. So I’m guessing I should wrap an Assert.Throws method around the presenter.Save() method call, but I thought the presenter.Save method should be called in the .Do(() => ...

请问我的单元测试应该是下面还是其他形式?

Can you please advise if my unit test should look like below or something else?

"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());

非常感谢

推荐答案

我会这样:

"Given a Options presenter"
    .Context(() =>
        presenter = new OptionsPresenter(view,
                                         (IOptionsModel)null,
                                         service));

"with the Save method called to save the option values"
    .Do(() => 
        exception = Record.Exception(() => presenter.Save()));

"expect an ValidationException to be thrown"
    .Observation(() =>
        Assert.IsType<ValidationException>(exception)
     );

"expect an service.SaveOptions method not to be called"
    .Observation(() =>
        A.CallTo(() => service.SaveOptions(A<IOptionsModel>.Ignored)).MustNotHaveHappened()
     );

或者更好的是,为 xBehave.net 切换SubSpec并引入

Or better still, switching SubSpec for xBehave.net and introducing FluentAssertions:-

"Given an options presenter"
    .x(() => presenter = new OptionsPresenter(view, (IOptionsModel)null, service));

"When saving the options presenter"
    .x(() => exception = Record.Exception(() => presenter.Save()));

"Then a validation exception is thrown"
    .x(() => exception.Should().BeOfType<ValiationException>());

"And the options model must not be saved"
    .x(() => A.CallTo(() =>
        service.SaveOptions(A<IOptionsModel>.Ignored)).MustNotHaveHappened());

这篇关于如何测试使用xUnit,SubSpec和FakeItEasy引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆