RSpec:使用嵌套资源时无法将图像转换为字符串
我在 RSpec 视图测试方面遇到问题。我正在使用嵌套资源和具有belongs_to 关联的模型。
这是我到目前为止所得到的:
describe "/images/edit.html.erb" do
include ImagesHelper
before(:each) do
@image_pool = stub_model(ImagePool, :new_record => false,
:base_path => '/')
assigns[:image] = @image =
stub_model(Image,
:new_record? => false,
:source_name => "value for source_name",
:image_pool => @image_pool)
end
it "renders the edit image form" do
render
response.should have_tag("form[action=#{image_path(@image)}][method=post]") do
with_tag('input#image_source_name[name=?]', "image[source_name]")
end
end
end
我收到的错误:
ActionView::TemplateError in '/images/edit.html.erb renders the edit image form'
can't convert Image into String
On line #3 of app/views/images/edit.html.erb
1: <h1>Editing image</h1>
2:
3: <% form_for(@image) do |f| %>
4: <%= f.error_messages %>
5:
6: <p>
app/views/images/edit.html.erb:3
/opt/dtcm/railstest/lib/ruby/gems/1.9.1/gems/rspec-rails-1.3.2/lib/spec/rails/extensions/action_view/base.rb:27:in `render_with_mock_proxy'
/opt/dtcm/railstest/lib/ruby/gems/1.9.1/gems/rspec-rails-1.3.2/lib/spec/rails/example/view_example_group.rb:170:in `render'
查看发生异常的 Rails 代码并不是很能说明问题。关于如何缩小这里发生的事情的范围有什么想法吗?
我尝试过的一件事是直接从示例中调用 form_for ,但我收到了一个不同的错误,抱怨缺少在 Spec::Rails::Example::ViewExampleGroup::Subclass_4:0xblah 上定义的“polymorphic_path”。不确定这是否真的意味着什么。
I'm having trouble with and RSpec view test. I'm using nested resources and the model with a belongs_to association.
Here's what I have so far:
describe "/images/edit.html.erb" do
include ImagesHelper
before(:each) do
@image_pool = stub_model(ImagePool, :new_record => false,
:base_path => '/')
assigns[:image] = @image =
stub_model(Image,
:new_record? => false,
:source_name => "value for source_name",
:image_pool => @image_pool)
end
it "renders the edit image form" do
render
response.should have_tag("form[action=#{image_path(@image)}][method=post]") do
with_tag('input#image_source_name[name=?]', "image[source_name]")
end
end
end
The error I'm receiving:
ActionView::TemplateError in '/images/edit.html.erb renders the edit image form'
can't convert Image into String
On line #3 of app/views/images/edit.html.erb
1: <h1>Editing image</h1>
2:
3: <% form_for(@image) do |f| %>
4: <%= f.error_messages %>
5:
6: <p>
app/views/images/edit.html.erb:3
/opt/dtcm/railstest/lib/ruby/gems/1.9.1/gems/rspec-rails-1.3.2/lib/spec/rails/extensions/action_view/base.rb:27:in `render_with_mock_proxy'
/opt/dtcm/railstest/lib/ruby/gems/1.9.1/gems/rspec-rails-1.3.2/lib/spec/rails/example/view_example_group.rb:170:in `render'
Looking at the rails code where the exception occurs is not very revealing. Any ideas on how I can narrow down what is going on here?
One thing I tried was calling form_for directly from the example and I got a different error griping about lack of 'polymorphic_path' defined on Spec::Rails::Example::ViewExampleGroup::Subclass_4:0xblah. Not sure if that actually means anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。这与 Rspec 无关,与正确使用嵌套资源和 Rails 助手有关。
显然,处理视图中嵌套资源的正确方法是:
只是错误消息没有帮助。
Ok. This has nothing to do with Rspec and everything to do with proper use of nested resources and rails helpers.
Apparently the right way to handle nested resources in a view is:
It's just that the error message isn't helpful.