Rails で rspec と factory girl を使用したテストを学習していますが、うまく動作させることができません。
私のユーザーコントローラは次のようになります:
class UsersController < ApplicationController
def index
@users = User.all.order(:first_name)
end
end
そしてテスト:
require 'spec_helper'
describe UsersController do
before(:each) do
@user1 = FactoryGirl.create(:user, first_name: "B", last_name: "B", uid: "b")
@user2 = FactoryGirl.create(:user, first_name: "A", last_name: "A", uid: "a")
end
describe "GET index" do
it "sets a list of users sorted by first name" do
get :index
assigns(:users).should == [@user2, @user1]
end
end
end
しかし、テストは以下を返します:
UsersController GET index sets a list of users sorted by first name
Failure/Error: assigns(:users).should == [@user2, @user1]
expected: [#<User id: nil, email: nil, first_name: "A", last_name: "A", uid: "a", active: true, admin: false, created_at: nil, updated_at: nil, reset_date: nil>, #<User id: nil, email: nil, first_name: "B", last_name: "B", uid: "b", active: true, admin: false, created_at: nil, updated_at: nil, reset_date: nil>]
got: nil (using ==)
# ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
私が間違っていることを知っていますか?
乾杯!
「レーキルート」は次のとおりです。
Prefix Verb URI Pattern Controller#Action
root GET / meetings#index
meetings GET /meetings(.:format) meetings#index
login GET /login(.:format) sessions#new
logout GET /logout(.:format) sessions#destroy
POST /auth/:provider/callback(.:format) sessions#create
auth_failure GET /auth/failure(.:format) sessions#failure
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy