Rspec:测试实例变量的分配 [英] Rspec: testing assignment of instance variable

查看:38
本文介绍了Rspec:测试实例变量的分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 Rspec 与 Factory Girl 一起使用.试图检查我的控制器中分配了哪些数据(并对其进行测试).我读过的每一篇文章都说我应该能够从assigns()中得到一些东西,但它一直返回nill

控制器

def 索引@stickies = Sticky.where(:user_id => current_user.id)结尾

规格

它应该分配粘性"做foo =assigns(:stickies)puts "foo = #{foo}"结尾

输出

foo =

我是否使用了错误的语法?有一个更好的方法吗?谢谢!!

解决方案

必须先调用动作

<预><代码>描述 StickiesController 做描述获取索引"做它应该分配粘性"做获取:索引assigns(:stickies).should_not be_nil结尾结尾结尾

Using Rspec with Factory Girl. Trying to check out what data is being assigned in my controller (and test against it). Every post I've read says I should be able to get something out of assigns() but it keeps returning nill

Controller

def index
 @stickies = Sticky.where(:user_id => current_user.id)
end

Spec

it "should assign stickies" do
  foo = assigns(:stickies)
  puts "foo = #{foo}"
end

Output

foo = 

Am I using the wrong syntax? Is there a better way to do this? Thanks!!

解决方案

You have to invoke the action first


describe StickiesController do
  describe "GET index" do
    it "should assign stickies" do
      get :index
      assigns(:stickies).should_not be_nil
    end
  end
end

这篇关于Rspec:测试实例变量的分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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