ruby-on-rails – 如何使用Capybara Rspec Matchers测试演示者?
发布时间:2020-12-17 04:00:01 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Capybara RSpec匹配器测试演示者方法. 让我们说我有一个呈现按钮的方法.如果我没有使用capybara rspec匹配器,这将是我要写的测试: it "should generate a button" do template.should_receive(:button_to).with("Vote"). and_return("THE_HTM
我正在尝试使用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 使用capybara rspec匹配器,我想这样做: it "should render a vote button" do subject.render_controls.should have_button('Vote') end 这篇文章是在http://devblog.avdi.org/2011/09/06/making-a-mockery-of-tdd/文章中提出的.在文章中,作者解释如下:“我决定更改我的规范设置,以传递一个模板对象,其中包含实际的Rails标记助手.然后我包括Capybara规范用于对HTML进行断言.“ 但是,我不明白这一点.当render_controls只返回content_tag时,如何使用capybara rspec匹配器? 解决方法
查看Capybara.string方法:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara#string-class_method
使用这种方法,你应该能够写出类似的东西: subject { Capybara.string(presenter.render_controls } it { should have_button('Vote') } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |