简体   繁体   English

Rails / RSpec:在观察者规范中使用:assigns?

[英]Rails / RSpec: Using :assigns in an observer spec?

I'm logging the changes to a model to use in an activity feed. 我正在将对模型的更改记录到活动供稿中使用。 I have an observer that does this (activerecord dirty): 我有一个这样做的观察者(activerecord很脏):

def before_update(derp)
  @changes = derp.changes
end

This abstracts this functionality out of the controller. 这将从控制器中提取此功能。 I want to test this using rspec, so I have this: 我想使用rspec对此进行测试,所以我有以下内容:

it "assigns derp.changes to @changes" do
  derp.attribute = 5
  @observer.before_update(derp)
  expect(assigns(:changes)).to eq(derp.changes)
end

I'm getting this error: undefined method `assigns' for RSpec::ExampleGroups::DerpObserver::BeforeUpdate:0x007fc6e24eb7f8 我收到此错误:RSpec :: ExampleGroups :: DerpObserver :: BeforeUpdate:0x007fc6e24eb7f8的未定义方法`assigns'

How can I use assigns in an observer spec? 如何在观察者规范中使用分配? Or, is there another way that I could test that @changes got assigned? 或者,还有另一种方法可以测试@changes是否已分配?

instance_variable_get is what I was looking for. instance_variable_get是我一直在寻找的东西。

http://apidock.com/ruby/Object/instance_variable_get http://apidock.com/ruby/Object/instance_variable_get

expect(@observer.instance_variable_get(:@changes)).to eq(derp.changes)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM