Rails,RSpec和Webrat:预期的输出与渲染的输出匹配,但在视图规格中仍然出现错误 [英] Rails, RSpec and Webrat: Expected output matches rendered output but still getting error in view spec
问题描述
我刚刚开始将BDD与RSpec/Cucumber/Webrat和Rails结合使用,但为了使我的视图规范通过而感到有些沮丧.
I've just gotten started using BDD with RSpec/Cucumber/Webrat and Rails and I've run into some frustration trying to get my view spec to pass.
首先,我在运行Rails 2.3.2,RSpec和RSpec-Rails 1.2.6,Cucumber 0.3.11和Webrat 0.4.4的Ruby 1.9.1p129.
First of all, I am running Ruby 1.9.1p129 with Rails 2.3.2, RSpec and RSpec-Rails 1.2.6, Cucumber 0.3.11, and Webrat 0.4.4.
这是与我的问题相关的代码
Here is the code relevant to my question
config/routes.rb:
map.b_posts 'backend/posts',
:controller => 'backend/posts',
:action => 'backend_index',
:conditions => { :method => :get }
map.connect 'backend/posts',
:controller => 'backend/posts',
:action => 'create',
:conditions => { :method => :post }
views/backend/posts/create.html.erb:
<% form_tag do %>
<% end %>
spec/views/backend/posts/create.html.erb_spec.rb:
describe "backend/posts/create.html.erb" do
it "should render a form to create a post" do
render "backend/posts/create.html.erb"
response.should have_selector("form", :method => 'post', :action => b_posts_path) do |form|
# Nothing here yet.
end
end
end
这是我运行脚本/规范时输出的相关部分:
Here is the relevant part of the output when I run script/spec:
'backend/posts/create.html.erb should render a form to create a post' FAILED
expected following output to contain a <form method='post' action='/backend/posts'/> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><form action="/backend/posts" method="post">
</form></body></html>
在我看来,have_selector寻找的正是模板生成的内容,但是该示例仍然失败.我非常期待看到我的错误(因为我感觉这是我的错误).任何帮助深表感谢!
It would appear to me that what have_selector is looking for is exactly what the template generates, yet the example still fails. I am very much looking forward to seeing my error (because I have a feeling it is my error). Any help is much appreciated!
推荐答案
如果要保留该块,请尝试使用rspec-rails匹配器而不是webrat匹配器.
If you want to keep the block try using rspec-rails matchers instead of the webrat matchers.
describe "backend/posts/create.html.erb" do
it "should render a form to create a post" do
render "backend/posts/create.html.erb"
response.should have_tag("form[method=post][action=?]", b_posts_path) do |form|
with_tag('input')
# ... etc
end
end
end
这篇关于Rails,RSpec和Webrat:预期的输出与渲染的输出匹配,但在视图规格中仍然出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!