2016-02-07 40 views
2

我有一个关于在水豚here干燥问题。汤姆回答完美,并在他提到的答案中:功能规格和视图规格是否有区别?

功能测试应该用于测试系统中较大的行为。

Ruby on Rails中的功能规范和视图规范是否有区别?如果可能,请用一些例子来解释它。 谢谢。

回答

2

是的,功能和视图规格是完全不同的。第一个是完整的集成测试,第二个是单独测试视图。

A 功能规格使用无头浏览器从外部测试整个系统,就像用户使用它一样。它也会练习代码,数据库,视图和Javascript,如果你使用正确的无头浏览器并打开Javascript。

与其他类型的rspec-rails规格不同,功能规格是使用featurescenario方法定义的。

功能规格,只有功能规格,使用水豚的所有功能,包括visit,方法如fill_inclick_button,以及像have_text匹配。

the rspec-rails documentation for feature specs中有很多例子。这里有一个快速之一:

feature "Questions" do 
    scenario "User posts a question" do 
    visit "https://stackoverflow.com/questions/ask" 
    fill_in "title" with "Is there any difference between a feature spec and a view spec?" 
    fill_in "question" with "I had a question ..." 
    click_button "Post Your Question" 
    expect(page).to have_text "Is there any difference between a feature spec and a view spec?" 
    expect(page).to have_text "I had a question" 
    end 
end 

一个视图规范只是呈现在隔离观察的图,通过测试,而不是由控制器提供的模板变量。

与其他类型的rspec-rails规格一样,查看规格是使用describeit方法定义的。一个用assign赋值模板变量,用render渲染视图,并用rendered得到结果。

视图规范中唯一使用的水豚功能是匹配器,如have_text

the rspec-rails documentation of view specs有很多例子。这里有一个快速之一:

describe "questions/show" do 
    it "displays the question" do 
    assign :title, "Is there any difference between a feature spec and a view spec?" 
    assign :question, "I had a question" 
    render 

    expect(rendered).to match /Is there any difference between a feature spec and a view spec\?/ 
    expect(rendered).to match /I had a question/ 
    end 
end 
+0

只是为了在这里澄清一个小错误 - 水豚是不是用来驱动在视图中测试的任何行动,它的匹配器是默认提供这样的事情,比如'希望(渲染)。为了have_text ('我有个问题')'或'expect(呈现).to have_css('#my_box.some_class')'会起作用 –

+0

好的一点,谢谢。更新。 –