【问题标题】:Is there any difference between a feature spec and a view spec?功能规范和视图规范之间有什么区别吗?
【发布时间】:2016-02-07 20:18:28
【问题描述】:

我有一个关于 Capybara here 内干燥的问题。汤姆回答得很完美,他在回答中提到:

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

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

【问题讨论】:

    标签: ruby-on-rails rspec capybara bdd rspec-rails


    【解决方案1】:

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

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

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

    功能规格,并且只有功能规格,使用 Capybara 的所有功能,包括 visitfill_inclick_button 等方法,以及 have_text 等匹配器。

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

    feature "Questions" do
      scenario "User posts a question" do
        visit "/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 获取结果。

    视图规范中使用的唯一 Capybara 功能是匹配器,例如 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
    

    【讨论】:

    • 在这里澄清一个小错误 - 虽然 Capybara 不用于驱动视图测试中的任何操作,但默认情况下它的匹配器可用,因此 expect(rendered).to have_text('I had a question')expect(rendered).to have_css('#my_box.some_class') 之类的东西可以工作跨度>
    猜你喜欢
    • 2018-05-08
    • 2010-12-25
    • 2019-04-20
    • 2011-04-01
    • 2016-09-22
    • 2014-08-11
    • 1970-01-01
    • 2015-08-28
    相关资源
    最近更新 更多