Rails,RSpec和Webrat:预期输出与渲染输出匹配,但在视图规范中仍然出现错误

时间:2009-06-21 23:40:37

标签: ruby-on-rails rspec webrat

我刚刚开始使用带有RSpec / Cucumber / Webrat和Rails的BDD,并且我试图让我的视图规范通过时遇到了一些挫折感。

首先,我运行Ruby 1.9.1p129,包含Rails 2.3.2,RSpec和RSpec-Rails 1.2.6,Cucumber 0.3.11和Webrat 0.4.4。

以下是与我的问题相关的代码

配置/ 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 }

视图/后端/帖/ create.html.erb:

<% form_tag do %>
<% end %>

规格/视图/后端/帖/ 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

以下是运行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正在寻找的正是模板生成的内容,但示例仍然失败。我非常期待看到我的错误(因为我感觉它是我的错误)。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

如果你想保持阻止,请尝试使用rspec-rails匹配器而不是webrat匹配器。

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