Chr*_*ini 3 testing rspec capybara ruby-on-rails-3 ruby-on-rails-3.1
我有一些关于水豚的问题.我不妨在这里问一下,因为Capybara的github页面中的RDOC 非常适合设置和运行.但是API或可用方法列表在哪里?
第一.Per*_spec.rb文件应该scenario只存在一次?或者scenario在一个文件中有多个?
例如,在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
Run Code Online (Sandbox Code Playgroud)
如果上面的代码结构有任何问题,或者还有改进的余地.我是公开反馈.
第二.我注意到上面的代码.如果我config.use_transactional_fixtures = false在spec/spec_helper.rb,它节省了用户的两倍.这意味着,在我的测试数据库/用户表中,我将有2个名为'foo bar'的用户.这是正常的吗?
第三.我有一个带有HTML按钮的表单.当用户单击此按钮时,jQuery会提交表单.我如何用Capybara测试这个?我认为click_button "Add"不会这样做.
第四.我如何在Capybara登录用户?我正在使用Devise.会sign_in User.first做的伎俩?我可以current_user在Capybara 进入吗?
最后,如果有人知道关于Rspec + Capybara的任何"入门"指南/教程.请提一下.
自从我决定不再喜欢Cucumber之后,我也转而写了请求规范.
ONE)有多个场景确实没问题.你可以使用rspec的所有其他强大功能,所以我建议你也使用底层代码中的上下文.
TWO)这可以通过使用Rspec Set Gem和Database Cleaner Gem 来解决.另外: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
Run Code Online (Sandbox Code Playgroud)
三)是的!click_button"添加"应该工作! 完整的capybara API很有用但是花了一些时间来讨论.最重要的是行动和rspec匹配器.
例:
click_button "Add"
page.should have_content("Successfully Added")
Run Code Online (Sandbox Code Playgroud)
你可以使用元素查找器缩小范围.
第四个)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
Run Code Online (Sandbox Code Playgroud)
当然,最终你想要将其分解为更具体的功能.可能有一个"公共导航"功能,用于所有关于访客看到内容的测试,然后是用户登录的单独功能,等等.
| 归档时间: |
|
| 查看次数: |
1312 次 |
| 最近记录: |