我正在尝试为子控制器编写一些控制器规范,在本例中为 Admin::UsersController
它具有基本的 CRUD 操作集。
我的 users_controller_spec.rb
describe Admin::CarriersController do
before(:each) do
sign_in FactoryGirl.create(:admin)
end
it "should have a current_user" do
subject.current_user.should_not be_nil
end
describe "GET 'index'" do
it "assigns all users as @users" do
user = create(:user)
get :index
assigns(:users).should eq [user]
end
it "renders the index view" do
get :index
expect(response).to render_template :index
end
end
end
现在我遇到的问题是索引操作。我的控制器工作并且是一个简单的@users = User.all
让事情复杂的是我的用户表是 STI 所以
class User < ActiveRecord::Base
end
class Client < User
end
class Seller < User
end
我的工厂
FactoryGirl.define do
factory :user do
name { Faker::Company.name }
sequence(:email) {|n| "test#{n}@test.com"}
password "password"
password_confirmation {|instance| instance.password }
type "Seller"
factory :admin do
type "Admin"
end
factory :seller do
type "Seller"
end
factory :client do
type "Client"
end
end
end
显然 eq 方法不起作用,因为 RSpec 在我的 assigns(:users) 期望中匹配类名时存在问题。我的确切错误是:
1) Admin::UsersController GET 'index' assigns all users as @users
Failure/Error: assigns(:users).should eq user
expected #<ActiveRecord::Relation [#<Client id: 1282, name: "Marks-Kozey", type: "Client"...]> to eq #<User id: 1282, name: "Marks-Kozey", type: "Client"...
我的问题是我的工厂吗?还是我测试不正确。这是我第一次测试 STI,所以任何帮助都会不胜感激。