我对 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
所以一切正常,除了该行assigns(: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)>'
将不胜感激任何提示,因为我已经花了一整天的时间。