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

我刚刚开始使用BDD与RSpec / Cucumber / Webrat和Rails,我遇到了一些挫败试图让我的视图规范通过.

首先,我使用Rails 2.3.2,RSpec和RSpec-Rails 1.2.6,Cucumber 0.3.11和Webrat 0.4.4运行Ruby 1.9.1p129.

这是与我的问题相关的代码

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

以下是运行脚本/规范时输出的相关部分:

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

最佳答案 如果你想保持阻止尝试使用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
点赞