第一. Per * _spec.rb文件,应该只存在一次?或者在一个文件中有多个场景可以吗?
例如,在spec / request / user_spec.rb中:
require 'spec_helper'
feature 'User actions' do
background do
data = {
:first_name => 'foo',
:last_name => 'bar',
...
}
user = User.new(data, :as => :user)
user.save
end
scenario 'User can browse home page' do
visit root_path
page.should have_content('Homepage')
end
scenario 'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
如果上面的代码结构有任何问题,或者还有改进的余地.我是公开反馈.
第二.我注意到上面的代码.如果我在spec / spec_helper.rb中有config.use_transactional_fixtures = false,它会将用户保存两次.这意味着,在我的测试数据库/用户表中,我将有2个名为’foo bar’的用户.这是正常的吗?
第三.我有一个带有HTML按钮的表单.当用户单击此按钮时,jQuery会提交表单.我如何用Capybara测试这个?我不认为click_button“添加”会做到这一点.
第四.我如何在Capybara登录用户?我正在使用Devise. sign_in User.first会这样做吗?我能够访问Capybara的current_user吗?
最后,如果有人知道任何关于Rspec Capybara的“入门”指南/教程.请提一下.
自从我决定不再喜欢Cucumber之后,我也转而写了请求规范.ONE)有多个场景确实没问题.你可以使用rspec的所有其他强大功能,所以我建议你也使用底层代码中的上下文.
TWO)这可以通过使用Rspec Set Gem和数据库清洁宝石来解决.另外:The Original Rationale for Set
警告:确保在使用set时正确设置DatabaseCleaner.我自己的设置(这可能有点矫枉过正,但对我有用):
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:suite) do
DatabaseCleaner.clean_with :truncation
end
三)是的! click_button“添加”应该工作! The complete capybara API很有用,但是我花了一些时间才知道.最重要的是行动和rspec匹配器.
例:
click_button "Add"
page.should have_content("Successfully Added")
你可以使用元素查找器缩小范围.
第四个)Devise提供帮助.有一个sign_in助手.阅读dox :).这是一个演示:
feature 'User actions' do
background do
data = {
:first_name => 'foo',
:last_name => 'bar',
...
}
@user = User.new(data, :as => :user)
@user.save
end
context "no user is signed in" do
scenario 'User can browse home page' do
visit root_path
page.should have_content('Homepage')
end
scenario 'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
context "user is signed in" do
before :each do
sign_in @user
end
[more scenarios]
end
end
当然,最终你想要将其分解为更具体的功能.可能有一个“公共导航”功能,用于所有关于访客看到内容的测试,然后是用户登录的单独功能,等等.
