ruby-on-rails – RSpec指定(:items).should eq([item])返回nil
发布时间:2020-12-17 03:54:51 所属栏目:百科 来源:网络整理
导读:我对RSpec很新,显然做错了. 拥有索引操作的基本控制器: class TransactionsController ApplicationController before_filter :authenticate_user! def index @transactions = current_user.transactions respond_to do |format| format.html { render layou
我对RSpec很新,显然做错了.
拥有索引操作的基本控制器: class TransactionsController < ApplicationController before_filter :authenticate_user! def index @transactions = current_user.transactions respond_to do |format| format.html { render layout: "items" } # index.html.erb format.json { render json: @transactions } end end end 并对Index操作进行默认的RSpec测试. require 'spec_helper' describe TransactionsController do describe "GET index" do it "assigns all transactions as @transactions" do transaction = FactoryGirl.create(:transaction) get :index assigns(:transactions).should eq([transaction]) end end end 所有工作,除了行分配(:transactions).should eq([transaction])返回nil,所以我得到这个错误: Failure/Error: assigns(:transactions).should eq([transaction]) expected: [#<Transaction id: 1,user_id: 1,text: "some transaction",date: "2013-02-02",money: #<BigDecimal:b8a8e90,'0.999E1',18(18)>,created_at: "2013-02-02 09:17:42",updated_at: "2013-02-02 09:17:42">] got: nil (compared using ==) # ./spec/controllers/transactions_controller_spec.rb:17:in `block (3 levels) in <top (required)>' 非常感谢任何提示,因为我花了一整天时间. 解决方法
首先,如果你使用before_filter:authenticate_user!你需要传递那个过滤器.
这可以通过使用sign_in some_user来完成 其次,当您分配事务@transactions = current_user.transactions时 并且您希望规范中应该有一些,您将必须创建分配给当前用户FactoryGirl.create(:transaction,user:some_user)的事务 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |