1
我试图为子控制器编写一些控制器规格,在这种情况下Admin :: UsersControllerRspec控制器与STI用户模型的测试?
它具有基本的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在匹配我的分配(:用户)期望中的类名时遇到了问题。 我的确切错误是:
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,所以任何帮助都是值得赞赏的。
为什么不通过工厂而是用户创建客户端? – gylaz 2013-03-27 04:29:03
是的,我最终将用户工厂分成了不同的用户类型的工厂,但这似乎是一个黑客,因为它不代表正确的STI,尽管现在工作 – TheIrishGuy 2013-03-27 13:30:56