4

我刚刚开始将 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。

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

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

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

<% form_tag do %>
<% end %>

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

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

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

4

1 回答 1

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
于 2009-07-10T15:14:47.450 回答