【问题标题】:rspec-rails: testing a controller action that renders a partialrspec-rails:测试呈现部分的控制器操作
【发布时间】:2013-09-29 01:23:37
【问题描述】:

我有一个具有 indexnew 操作的 UsersController。我用的是haml,索引haml文件包含以下代码:

= link_to 'Add User', new_user_path, { :remote => true, 'data-toggle' => 'modal', 
  'data-target' => '#modal-window', 'class' => 'btn btn-primary pull-right' }

因此,当用户单击“添加用户”时,_new.html.haml 部分将作为模式呈现给用户。它工作得很好。这是控制器中的新操作:

def new
  @user = User.new

  respond_to do |format|
    format.html
    format.js
  end
end

现在在我的控制器规范中,我正在尝试执行以下操作:

describe "new action" do
  before do
    get 'new'
  end

  # specs for 'new' action go here
end

但是会报错

Failure/Error: get 'new'
 ActionView::MissingTemplate:
   Missing template users/new, application/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
     * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007faa44b03218>"

大概是因为它找不到new.html.haml,这当然是不存在的,因为该文件是一个名为_new.html.haml 的部分文件。 getting 部分的正确语法是什么?如果没有,我该如何测试new 操作?

【问题讨论】:

  • 你的控制器不应该渲染视图吗?
  • @LeoCorrea 好点。我将 format.html 更改为 format.html { render :partial =&gt; 'new' } 并且它有效。把它放在一个答案中,我会接受它。
  • 我不认为这是我的答案:)。你自己找到的。您可以回答自己的问题并将其标记为正确。很高兴你能找到答案。
  • 好的,那么。感谢您的评论,如果我没有找到它,我相信评论会做到的。

标签: ruby-on-rails-3 rspec2 partials


【解决方案1】:

好的,这是我在新操作中所做的更改以使其正常工作:

respond_to do |format|
  format.html { render :partial => 'new' }
  format.js
end

当应用程序运行时,rails 会计算出渲染部分,但我想它需要对 rspec 明确。如果有更好的方法,我会很高兴听到它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多