导轨:“部分缺失”当调用“render”时;在 RSpec 测试中

发布于 2024-10-07 06:54:25 字数 1112 浏览 4 评论 0原文

我正在尝试测试表单是否存在。我是 Rails 新手。

我的 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

视图本身 new.html.erb 具有代码:

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

当我运行 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)>'

有谁知道问题是什么?

谢谢!

I'm trying to test for the presence of a form. I'm new to Rails.

My new.html.erb_spec.rb file's contents are:

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:

<%= 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:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┼── 2024-10-14 06:54:25

不要对“渲染”给出任何参数。尝试以下操作

require 'spec_helper'

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

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
病毒体 2024-10-14 06:54:25

我遇到了同样的问题,如果您还没有找到解决方案,我将描述我为解决此问题所做的事情。
我认为问题是 rspec 在这里报告了一个误导性错误。真正的错误是别的东西。

我通过首先将该行更改为:

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

这使得实际错误浮出水面。就我而言,我没有设置所需的变量,我对此进行了更正。

一旦正确设置了分配,规范就可以正常运行。

然后你可以回到其中一个:

render "messages/new.html.erb"

或者甚至

render

两者都可以。缺少模板的问题应该消失了。至少对我来说是这样。 :)

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. 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. :)

栩栩如生 2024-10-14 06:54:25

我遇到了类似的问题,并惊讶地发现 Rspec 从描述参数中做出了一些令人印象深刻的推论。例如:

require 'spec_helper'

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

由于一些进化的控制器/视图名称,这个测试最初有:

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

这真的把一切搞乱了,即使我在渲染后设置了:模板,它也无法找到引用的部分。修复描述语句中的路径就解决了这一切。

I ran into a similar issue, and was surprised to learn that Rspec does some impressive inference from the describe argument. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文