【问题标题】:rspec rendered view fails to include variable datarspec 渲染视图无法包含可变数据
【发布时间】:2012-08-14 04:44:06
【问题描述】:

我的 rspec 视图测试失败,但代码有效 - 我可能有一个变量设置不正确,但无法弄清楚它是什么。

当我在我的规范中显示@incident_report (pp @incident_report) 的内容时,它会正确显示 FactoryGirl 创建的记录。

当我显示实际渲染的内容(渲染的放置)时,它会显示我使用 FactoryGirl 创建的记录中的值...

但是“rendered.should contains(work_order)”规范失败了:

1) incident_reports/show.html displays the work order number on the incident
   Failure/Error: rendered.should contain(work_order)
     expected the following element's content to include "54785":

不显示任何数据,只显示 HTML 模板


spec/views/incident_report/show.html.haml_spec.rb 代码

require 'spec_helper'

describe "incident_reports/show.html" do
  before(:each) do
    @incident_report = Factory(:incident_report)
  end

  it "displays the work order number on the incident" do
    work_order = @incident_report.work_order
    pp @incident_report    #displays an incident_report, id => 1
    assign(:incident_report, @incident_report)
    render 
    puts rendered  #this DOES have the content from @incident_report
    rendered.should contain("Work Order:")
    rendered.should contain(work_order)
    end
  end

show.html.haml 代码

%h1 Display Incident Report
.navigation
  = link_to 'Edit', edit_incident_report_path(@incident_report)
  |
  \#{link_to 'Back', incident_reports_path}

= render 'form'

.navigation
  = link_to 'Edit', edit_incident_report_path(@incident_report)
  |
  \#{link_to 'Back', incident_reports_path}

必须是一些我忽略的非常简单的东西。

【问题讨论】:

  • 这是不久前的事了,所以我猜你已经整理好了,但我要检查几件事以确保 work_order 变量具有你所期望的值 @987654324 @。此外,如果您的 work_order 不包含字符串,我不确定 contains 有多聪明,可能值得做类似rendered.should contain(work_order.to_s) 的事情

标签: ruby-on-rails-3 rspec views


【解决方案1】:

原来是因为我使用的是 simple_form,当 simple_form 显示“显示”操作时,它会将字段值作为 'value="54785"' 属性放入 html 中。如果在浏览器中显示,标签和值都可以正确显示,但 rspec 看不到它们。

我必须添加

rendered.should have_tag 'input', :with => { :value => "54765", :name => 'incident_report[work_order]' }

我的例子让它工作。

似乎应该有更好的解决方案,但至少现在我可以继续测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2021-05-23
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多