关于水豚的几个问题
我有几个关于水豚的问题。我不妨在这里问一下,因为 Capybara 的 github 页面 中的 RDOC 非常适合设置它和跑步。但是 API 或可用方法列表在哪里?
首先。每个 *_spec.rb 文件,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
上面的代码结构是否有问题,或者是否有改进的空间。我开放反馈。
第二。我注意到上面的代码。如果我在 spec/spec_helper.rb
中有 config.use_transactional_fixtures = false
,它可以为用户节省两次。这意味着,在我的测试数据库/用户表中,我将有 2 个名为“foo bar”的用户。这是正常的吗?
第三。我有一个带有 HTML 按钮的表单。当用户单击此按钮时,jQuery 会提交表单。我如何用水豚测试这个?我认为 click_button "Add"
不会起作用。
第四。我如何在 Capybara 中登录用户?我正在使用设计。 sign_in User.first
可以解决问题吗?我可以在水豚中访问 current_user
吗?
最后,如果有人知道有关 Rspec + Capybara 的任何“入门”指南/教程。请务必提及。
I've got a few questions about Capybara. And I might as well ask here since the RDOC in the github page for Capybara is great to get it set up and running. But where is the API or list of available methods??
First. Per *_spec.rb file, should scenario
only exist once? Or is it fine to have multiple scenario
's in one file?
For example, in 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
If there is anything wrong with the code structure above, or if there is room for improvement. I am open feedback.
Second. I notice with the code above. If I have config.use_transactional_fixtures = false
in spec/spec_helper.rb
, it saves the user twice. This means, in my test database / user table, I would have 2 users named 'foo bar'. Is this normal?
Third. I have a form that has an HTML button. When user clicks on this button, jQuery submits the form. How would I test this with Capybara? I don't think click_button "Add"
will do the trick.
Fourth. How would I sign in users in Capybara? I am using Devise. Would sign_in User.first
do the trick? And would I be able to access current_user
in Capybara?
Lastly, if anyone knows any "Getting Started" guides / tutorials on Rspec + Capybara. Please do mention.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自从我决定不再喜欢 Cucumber 以来,我也转而编写请求规范。
一)拥有多个场景确实很好。您可以使用 rspec 的所有其他强大功能,因此我建议也使用上下文,如底部的代码所示。
二)这可能可以通过使用 Rspec Set Gem 和 Database Cleaner Gem 来解决。另外:设置的原始原理
警告:确保设置使用 set 时正确启动 DatabaseCleaner。我自己的设置(可能有点矫枉过正,但对我有用):
三)是的! click_button“添加”应该可以! 完整的 capybara API 很有用,但我花了一段时间才理解。最重要的是操作和 rspec 匹配器。
例如:
您可以使用元素查找器缩小范围。
第四)设计提供帮助者。有一个登录助手。阅读 dox :)。这是一个演示:
当然,最终您可能希望将其分成更具体的功能。可能有一个“公共导航”功能,用于所有有关访客查看内容的测试,然后为用户登录提供单独的功能,等等。
I've also switched over to writing request specs ever since i decided that I was no longer liking Cucumber.
ONE) Having multiple scenarios is indeed fine. You get to use all the other great features of rspec, so I would suggest also using contexts as in the code at the bottom.
TWO) This can probably be solved by using the Rspec Set Gem And the Database Cleaner Gem. Also: The Original Rationale for Set
Warning: make sure you set up DatabaseCleaner correctly when you use set. My own setup (which may be a little overkill but is working for me):
THREE) yep! click_button "Add" should work! The complete capybara API is useful but took me a while to grok. Of most relevant importance are the actions and rspec matchers.
example:
you can narrow the scope with element finders.
FOURTH) Devise provides helpers. there is a sign_in helper. read the dox :). Here's a demo:
ultimately of course, you'd prolly want to split this up into more specific features. Probably have a "Public Navigation" Feature for all the tests that are about guests seeing content, and then a separate feature for a user signing in, etc.
我不知道水豚,但可以在这里找到可用方法的完整列表:
http:// /rubydoc.info/github/jnicklas/capybara/master#
希望,这有帮助
I am not aware of capybara, but a full list of available methods can be found here:
http://rubydoc.info/github/jnicklas/capybara/master#
hope, that helps