简体   繁体   English

如何测试“新”控制器动作?

[英]How to test 'new' controller actions?

I am using Ruby on Rails 3.2.2, Rspec 2.9.0 and RspecRails 2.9.0. 我正在使用Ruby on Rails 3.2.2,Rspec 2.9.0和RspecRails 2.9.0。 I am trying to test a new controller action and I would like to know why I get the error explained above only for that action. 我正在尝试测试new控制器动作,我想知道为什么我得到上面仅针对该动作解释的错误。

Given: 鉴于:

# controller
class ArticlesController < ApplicationController
  before_filter :signed_in

  def new
    @article = Article.new

    # This is just a sample code line to show you where the error happens?
    @article.new_record?

    ...
  end

  def show
    @article = Article.find(params[:id])

    ...
  end
end

# spec file
require 'spec_helper'

describe ArticlesController do
  before(:each) do
    @current_user = FactoryGirl.create(:user)

    # Signs in user so to pass the 'before_filter'
    cookies.signed[:current_user_id] = {:value => [@current_user.id, ...]}
  end

  it "article should be new" do
    article = Article.should_receive(:new).and_return(Article.new)
    get :new
    assigns[:article].should eq(article)
  end

  it "article should be shown" do
    article = FactoryGirl.create(:article)

    get :show, :id => article.id.to_s

    assigns[:article].should eq(article)
  end
end

When I run the Example related to the new action I get this error (it is related to the @article.new_record? code line in the controller file): 当我运行与new操作有关的示例时,出现此错误(它与控制器文件中的@article.new_record?代码行有关):

Failure/Error: get :new
NoMethodError:
  undefined method `new_record?' for nil:NilClass

But when I run the Example related to the show action it passes without errors. 但是,当我运行与show动作相关的Example时,它通过时没有错误。

What is the problem? 问题是什么? How can I solve that? 我该如何解决?

The problem is the way you've done 问题是你做事的方式

Article.should_receive(:new).and_return(Article.new)

This is the same as 这和

temp = Article.should_receive(:new)
temp.and_return(Article.new)

So by the time you are setting up the return value, Article.new has already been mocked out and so returns nil, so you're doing and_return(nil) Create the return value first, ie 因此,当您设置返回值时, Article.new已经被and_return(nil)出来,因此返回nil,因此您正在执行and_return(nil)创建返回值,即

new_article = Article.new #or any other way of creating an article - it may also be appropriate to return a mock
Article.should_receive(:new).and_return(new_article)

Try: 尝试:

it "article should be new" do
  article = FactoryGirl.build(:article)
  Article.stub(:new).and_return(article)

  get :new

  assigns(:article).should == article
end

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

相关问题 如何用复杂的查询测试控制器动作? - How to test controller actions with complicated queries? 如何测试控制器操作中的简单错误 - How to test Controller Actions for simple mistakes 如何测试需要当前会​​话的控制器动作? - How to test controller actions that require a current session? Rspec:测试所有控制器动作 - Rspec: test for all controller actions 如何使用 request.headers 测试控制器中任何操作的回调? - How to test the callback for any actions in the controller using request.headers? 测试“创建”控制器操作的正确方法是什么? - What is the proper way to test 'create' controller actions? 如何测试before_filter将为所有Rails控制器操作重定向? - How do I test that a before_filter will redirect for all Rails controller actions? 如何测试Rails控制器的动作与RSpec调用正确的方法? - How can I test rails controller actions call proper methods with RSpec? Rails + simple_form + remote_true +自定义控制器:如何处理“ new”和“ edit”的不同表单动作? - Rails + simple_form + remote_true + custom controller: how to handle different form actions for 'new' and 'edit'? 如何使用Shoulda在Rails中测试控制器和新的AR创建? - how to use Shoulda to test to controller and new AR creation in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM