简体   繁体   English

Rails:在RSpec测试中调用'render'时“缺少部分”

[英]Rails: “missing partial” when calling 'render' in RSpec test

I'm trying to test for the presence of a form. 我正在尝试测试表单的存在。 I'm new to Rails. 我是Rails的新手。

My new.html.erb_spec.rb file's contents are: 我的new.html.erb_spec.rb文件的内容是:

require 'spec_helper'

describe "messages/new.html.erb" do
  it "should render the form" do
    render '/messages/new.html.erb'
    reponse.should have_form_putting_to(@message) 
    with_submit_button
  end
end

The view itself, new.html.erb, has the code: 视图本身new.html.erb具有以下代码:

<%= form_for(@message) do |f| %>
  <%= f.label :msg %> <br />
  <%= f.text_area :msg %>
  <%= f.submit "Submit" %>
<% end %>

When I run rspec , it fails as so: 当我运行rspec ,它会失败:

1) messages/new.html.erb should render the form

 Failure/Error: render '/messages/new.html.erb'

   Missing partial /messages/new.html with {:handlers=>[:erb, :rjs,:builder,:rhtml, :rxml], :formats=>[:html,:text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]} in view paths "/Users/tristanmartin/whisperme/app/views"

   # ./spec/views/messages/new.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'

Does anyone know what the problem is? 有谁知道问题是什么?

Thanks! 谢谢!

don't give any argument to 'render'. 不要给'渲染'任何参数。 try the following 尝试以下方法

require 'spec_helper'

describe "messages/new.html.erb" do
  it "should render the form" do
    render
    rendered.should contain('blablabla') 
  end
end

I experienced the same issue and I'll describe what I did to solve this, in case you haven't found the solution yet. 我遇到了同样的问题,我将描述我为解决这个问题所做的工作,以防您尚未找到解决方案。 I think the problem is rspec reports an misleading error here. 我认为问题是rspec在这里报告了一个误导性的错误。 The real error is something else. 真正的错误是别的。

I found this out by first changing the line to: 我通过首先将线路更改为:

render :template => "messages/new.html.erb"

This allowed the actual error to surface. 这允许实际错误浮出水面。 In my case, I was not setting up required variable(s), which I corrected. 在我的情况下,我没有设置必要的变量,我纠正了。

Once you have set the assigns correctly, spec ran properly. 正确设置分配后,规范就可以正常运行。

Then you may go back to either: 然后你可以回到:

render "messages/new.html.erb"

or even just 甚至只是

render

both will work. 两者都有效。 Missing template issue should be disappeared. 缺少模板问题应该会消失。 At least it did for me. 至少它对我有用。 :) :)

I ran into a similar issue, and was surprised to learn that Rspec does some impressive inference from the describe argument. 我遇到了类似的问题,并且惊讶地发现Rspec从describe参数中做了一些令人印象深刻的推论。 For example: 例如:

require 'spec_helper'

describe "bills/payments/edit.html.erb" do
  it "Renders payment form" do
    assign(:payment, stub_model(Payment))
    render
  end
end

Due to some evolutionary controller / view names, this test originally had: 由于一些进化控制器/视图名称,此测试最初具有:

describe "bills/payment/edit.html.erb" do

that really messed everything up, and even if I set the :template after render, it was unable to find a referenced partial. 这真的搞砸了一切,即使我在渲染后设置了:模板,也无法找到引用的部分。 Fixing up the path in the describe statement fixed it all. 修复describe语句中的路径修复了所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM