2012-09-12 21 views
27

私はdevise、rolify、cancanを使用しています。また、FactoryGirlでrspecを使ってテストしています。今私はいくつかのテストに取り組んでいます。これらのテストには異なる役割を持つユーザーを定義したいと思います。 は「spec_helper」を必要とFactoryGirl定義でrolifyを使用してロールを設定する

describe "Pages" do 
    subject { page } 

    before do 
     @newpage = FactoryGirl.create(:page) 
     @newpage.save 
    end 

    context 'not logged in' do 
     it_behaves_like 'not admin' 
    end 

    context 'logged in' do 

     context 'as user' do 
      before do 
       @user = FactoryGirl.create(:user) 
       sign_in @user 
      end 

      it_behaves_like 'not admin' 
     end 

     context 'as curator' do 
      before do 
       @curator = FactoryGirl.create(:curator) 
       sign_in @curator 
      end 

      it_behaves_like 'not admin' 
     end 

     context 'as admin' do 
      before do 
       @admin = FactoryGirl.create(:admin) 
       sign_in @admin 
      end 

      it_behaves_like 'admin' 
     end 

     context 'as super admin' do 
      before do 
       @super_admin = FactoryGirl.create(:super_admin) 
       sign_in @super_admin 
      end 

      it_behaves_like 'admin' 
     end 
    end 
end 

ときI:

FactoryGirl.define do 
    factory :user do 
    name 'Test User' 
    email '[email protected]' 
    password 'please' 
    password_confirmation 'please' 
    # required if the Devise Confirmable module is used 
    confirmed_at Time.now 

    factory :admin do 
     self.has_role :admin 
    end 

    factory :curator do 
     self.has_role :curator 
    end 

    factory :super_admin do 
     self.has_role :super_admin 
    end 
    end 
end 

をここに正しく書き込まれていないようです私のテストのいくつかである:ここでFactoryGirlを使用することを実行する方法のための私の現在の推測です

1) Pages logged in as admin behaves like admin can show page 
Failure/Error: Unable to find matching line from backtrace 
NoMethodError: 
    undefined method `has_role=' for #<User:0x007f883384d178> 
Shared Example Group: "admin" called from ./spec/requests/pages_spec.rb:41 
# ./spec/requests/pages_spec.rb:37:in `block (4 levels) in <top (required)>' 

2) Pages logged in as curator behaves like not admin can show page 
Failure/Error: Unable to find matching line from backtrace 
ArgumentError: 
    Factory not registered: curator 
Shared Example Group: "not admin" called from ./spec/requests/pages_spec.rb:32 
# ./spec/requests/pages_spec.rb:28:in `block (4 levels) in <top (required)>' 

3) Pages logged in as super admin behaves like admin can show page 
Failure/Error: Unable to find matching line from backtrace 
ArgumentError: 
    Factory not registered: super_admin 
Shared Example Group: "admin" called from ./spec/requests/pages_spec.rb:50 
# ./spec/requests/pages_spec.rb:46:in `block (4 levels) in <top (required)>' 

答えて

55

私はむしろまたを参照してください(ロールを作成するFactoryGirls after(:create)callbackを使用します。私はこれらが私のエラーであるスペックを実行しますRolifの号)。ユーザーが特定の役割を持っている場合はあなたがadd_roleメソッドを使用する必要があります特定の役割を設定することが

はさらに方法 has_role?は、 チェックに使用されています。

FactoryGirl.define do 
    factory :user do 
    name 'Test User' 
    email '[email protected]' 
    password 'please' 
    password_confirmation 'please' 
    # required if the Devise Confirmable module is used 
    confirmed_at Time.now 

    factory :admin do 
     after(:create) {|user| user.add_role(:admin)} 
    end 

    factory :curator do 
     after(:create) {|user| user.add_role(:curator)} 
    end 

    factory :super_admin do 
     after(:create) {|user| user.add_role(:super_admin)} 
    end 
    end 
end 
+0

ありがとう、私は次回これを試してみます。私はその後、ユーザーの役割を扱う別の方法に移りました。 –

+0

は魅力的に機能します! – SporkInventor

+0

偉大な答え!それは期待通りに機能します。 – Kulgar

関連する問題