e3m*_*eus 4 rspec ruby-on-rails presenter capybara
我正在尝试使用Capybara RSpec匹配器测试演示者方法.
让我们说我有一个呈现按钮的方法.如果我没有使用capybara rspec匹配器,这将是我要写的测试:
it "should generate a button" do
template.should_receive(:button_to).with("Vote").
and_return("THE_HTML")
subject.render_controls.should be == "THE_HTML"
end
Run Code Online (Sandbox Code Playgroud)
使用capybara rspec匹配器,我想这样做:
it "should render a vote button" do
subject.render_controls.should have_button('Vote')
end
Run Code Online (Sandbox Code Playgroud)
本文提出了这种方法http://devblog.avdi.org/2011/09/06/making-a-mockery-of-tdd/.在文章中,作者解释如下:"我决定更改我的规范设置,以便传递一个模板对象,其中包含实际的Rails标记助手.然后我包括了Capybara规范匹配器,用于对HTML进行断言".
但是,我不明白这一点.当render_controls只返回content_tag时,如何使用capybara rspec匹配器?
尽管luacassus的答案是正确的,但我发现了问题所在.我没有在测试中包括capybara rspec匹配器.如果你不包括Capybara rspec匹配器,你会出现这样的错误:undefined method has_selector?对于ActiveSupport :: SafeBuffer:0x9449590.
当您包含rspec匹配器时,不需要使用Capybara String方法,因为rspec匹配器已经与字符串匹配.
我在这里留下一个更详细的例子.
require_relative '../../app/presenters/some_presenter'
require 'capybara/rspec'
describe 'SomePresenter'
include Capybara::RSpecMatchers
let(:template) { ActionView::Base.new }
subject { Presenter.new(template) }
it "should render a vote button" do
subject.render_controls.should have_button('Vote')
end
end
Run Code Online (Sandbox Code Playgroud)
查看Capybara.string方法:http://rubydoc.info/github/jnicklas/capybara/master/Capybara#string-class_method
使用这种方法,你应该能够写出类似的东西:
subject { Capybara.string(presenter.render_controls }
it { should have_button('Vote') }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4604 次 |
| 最近记录: |