mar*_*ion 8 ruby rspec ruby-on-rails ruby-on-rails-4
在我Dashboard#Index,我有这个:
  def index        
    tagged_nodes = Node.includes(:user_tags).tagged_with(current_user.email)    
  end
如何使用RSpec进行测试?
我试过了:
  expect(assigns(tagged_nodes)).to match Node.includes(:user_tags).tagged_with(u1.email)
但这给了我这个错误:
 NameError:
       undefined local variable or method `tagged_nodes' for #<RSpec::ExampleGroups::DashboardController::GETIndex:0x007fe4edd7f058>
您不能(也不应该)测试局部变量.但是,您可以而且应该测试实例变量,这些变量是以开头的变量开头的@.为此,您使用assigns帮助程序,将实例变量的名称作为符号传递给它.如果我们想要实例变量的值@tagged_nodes,我们调用assigns(:tagged_nodes)(注意:).
因此,如果您的控制器方法如下所示:
def index        
  @tagged_nodes = Node.includes(:user_tags).tagged_with(current_user.email)    
end
...您将访问@tagged_nodes具有assigns(:tagged_nodes):
expect(assigns(:tagged_nodes))
  .to match Node.includes(:user_tags).tagged_with(u1.email)
| 归档时间: | 
 | 
| 查看次数: | 6770 次 | 
| 最近记录: |