如何使用 RSpec 测试在控制器中实例化的实例变量 [英] How to test instance variable was instantiated in controller with RSpec

查看:52
本文介绍了如何使用 RSpec 测试在控制器中实例化的实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查 RESTful 控制器中的新操作是否设置了所需对象类型的实例变量.看起来很典型,但执行起来有困难

客户端控制器

def new@client = Client.new结尾

测试

describe "GET 'new'" 做它应该成功"做获得新"response.should be_success结尾它应该创建一个新客户"做获得新"assigns(:client).should == Client.new结尾结尾

结果...

<前>'ClientsController GET 'new' 应该创建一个新的客户端'失败预期的: #,得到:#(使用==)

这可能是因为它试图比较两个不同的活动记录实例.那么,我如何检查控制器是否设置了一个实例变量来保存客户端模型的新实例.

解决方案

#rspec 中的技术泡菜帮了我...

assigns(:client).should be_kind_of(Client)

I'm trying to check that a new action in my RESTful controller set up an instance variable of the required Object type. Seems pretty typical, but having trouble executing it

Client Controller

def new
  @client = Client.new
end  

Test

describe "GET 'new'" do
  it "should be successful" do
    get 'new'
    response.should be_success
  end

  it "should create a new client" do
    get 'new'
    assigns(:client).should == Client.new
  end
end

Results in...

'ClientsController GET 'new' should create a new client' FAILED
  expected: #,
       got: # (using ==)

Which is probably because it's trying to compare 2 instances of active record, which differ. So, how do I check that the controller set up an instance variable that holds a new instance of the Client model.

解决方案

technicalpickles in #rspec helped me out...

assigns(:client).should be_kind_of(Client)

这篇关于如何使用 RSpec 测试在控制器中实例化的实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆