2012-06-13 41 views
13

Tôi đang sử dụng xUnit, SubSpec và FakeItEasy cho các bài kiểm tra đơn vị của mình. Tôi đã cho đến nay đã tạo ra một số xét nghiệm đơn vị tích cực như sau:Cách kiểm tra các trường hợp ngoại lệ được ném bằng xUnit, SubSpec và 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)); 

Nhưng bây giờ tôi muốn viết một số bài kiểm tra đơn vị tiêu cực và kiểm tra xem phương pháp nào đó không được gọi, và một ngoại lệ được ném

ví dụ

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

Tôi có thể thấy FakeItEasy có phương thức tiện ích mở rộng MustNotHave, và xUnit có phương thức Assert.Throws.

Nhưng làm thế nào để kết hợp tất cả lại với nhau?

Ngoại lệ tôi muốn kiểm tra sẽ xảy ra khi phương thức Lưu được gọi. Vì vậy, tôi đoán tôi nên bọc một phương pháp Assert.Throws xung quanh cuộc gọi phương thức presenter.Save(), nhưng tôi nghĩ phương pháp presenter.Save nên được gọi trong .Do (() => ...

bạn có thể vui lòng tư vấn cho nếu kiểm tra đơn vị của tôi sẽ giống như dưới đây hay cái gì khác?

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

rất cám ơn

+0

Không chắc điều này có thể giúp đỡ, nhưng bạn đã kiểm tra tài liệu trên SubSpec ví dụ https://bitbucket.org/johannesrudolph/subspec/src/a35fcc8ae1f6/test/SubSpec.Tests/ContextSetupTeardownBehavior.cs Ngoài ra đây là những Các bài kiểm tra dựa trên BDD/Đặc điểm kỹ thuật không phải là Bài kiểm tra đơn vị. Bạn có thể có được đối tượng tốt hơn nếu bạn bao gồm thẻ BDD. – Spock

Trả lời

6

tôi sẽ làm điều đó như thế này:

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

Hoặc tốt hơn vẫn còn, chuyển SubSpec cho xBehave.net và giới thiệu 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()); 
10

tôi đã không nghe nói về fakeItEasy hoặc subSpec (bạn kiểm tra đang trông khá sôi nổi, vì vậy tôi có thể kiểm tra chúng ra :)). Tuy nhiên, tôi sử dụng xUnit vì vậy đây có thể hữu ích:

tôi sử dụng Record.Exception với Assert.ThrowsDelegate

Vì vậy, một cái gì đó như:

[Fact] 
    public void Test() 
    { 
     // Arange 

     // Act 
     Exception ex = Record.Exception(new Assert.ThrowsDelegate(() => { service.DoStuff(); })); 

     // Assert 
     Assert.IsType(typeof(<whatever exception type you are looking for>), ex); 
     Assert.Equal("<whatever message text you are looking for>", ex.Message); 
    } 

Hy vọng rằng sẽ giúp.

2

Đây là một cách để làm điều đó trong FakeItEasy.

Action act =() => someObject.SomeMethod(someArgument); 
act.ShouldThrow<Exception>(); 
+0

Tôi nhận được một "Hành động không chứa một định nghĩa cho Shouldthrow ..." Tôi cần bao gồm tham chiếu/lắp ráp nào? – unekwu

Các vấn đề liên quan