gpt4 book ai didi

ruby-on-rails - Rails、RSpec 和 Webrat : Expected output matches rendered output but still getting error in view spec

转载 作者:行者123 更新时间:2023-12-01 04:22:01 24 4
gpt4 key购买 nike

我刚刚开始将 BDD 与 RSpec/Cucumber/Webrat 和 Rails 一起使用,但我在尝试通过 View 规范时遇到了一些挫折。

首先,我使用 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 }

View /后端/帖子/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

这是我运行脚本/规范时输出的相关部分:
'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

关于ruby-on-rails - Rails、RSpec 和 Webrat : Expected output matches rendered output but still getting error in view spec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1025036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com