ruby-on-rails – 功能规范和视图规范之间有什么区别吗?
我有一个关于Capybara
here干燥内部的问题.汤姆回答得很完美,在他的回答中他提到:
Ruby on Rails中的功能规范和视图规范之间有区别吗?如果可能请用一些例子解释一下. 解决方法
是的,功能和视图规格完全不同.第一个是完整集成测试,第二个是隔离测试视图.
功能规范使用无头浏览器从外部测试整个系统,就像用户使用它一样.如果您使用正确的无头浏览器并打开Javascript,它还会运行代码,数据库,视图和Javascript. 与其他类型的rspec-rails规范不同,功能规范是使用功能和方案方法定义的. 功能规格,仅限功能规格,使用Capybara的所有功能,包括访问,fill_in和click_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规范一样,视图规范是使用describe和it方法定义的.一个用assign分配模板变量,用render渲染视图并获得渲染结果. 视图规范中使用的唯一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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |