0

rspec で一時変数として使用される変数を渡すことによってユーザーを作成するにはどうすればよいですか? 私は何を間違っていますか?

controller_tests_spec.rb

describe "GET index" do
  context "with admin user_role" do
    before(:each) do
      set_current_admin
      todo_list1 = Fabricate(:todo_list)
      todo_list2 = Fabricate(:todo_list)
      get :index
    end
    it "redirects to the todo lists path" do
      expect(response).to redirect_to todo_lists_path
    end
  end
end

/support/macros.rb

def set_current_admin(user=nil)
  session[:user_id] = (user || Fabricate(:user, role: "admin")).id
end

製作者

  # user_fabricator.rb
  Fabricator(:user) do
    email {Faker::Internet.email}
    password 'password'
    first_name {Faker::Name.first_name}
    last_name {Faker::Name.last_name}
    transient :role  
    user_role {Fabricate(:user_role, role: :role)}
  end

  # user_role_fabricator.rb
  Fabricator(:user_role) do
    role
  end
4

1 に答える 1

3

処理されたすべての値 (トランジェントを含む) は、作成時に各フィールドに渡される attrs ハッシュを通じて利用できます。私はあなたが期待していると私が思うことをするためにあなたのファブリケーターを修正しました。

役割フィールドをデフォルトで:user_role「メンバー」に設定しました。あなたが持っていた方法は、 という名前の別のファブリケーターを使用するように拡張しようとしますがrole、それはあなたが望んでいるものではないと思います.

# user_fabricator.rb
Fabricator(:user) do
  email {Faker::Internet.email}
  password 'password'
  first_name {Faker::Name.first_name}
  last_name {Faker::Name.last_name}
  transient :role  
  user_role { |attrs| Fabricate(:user_role, role: attrs[:role]) }
end

# user_role_fabricator.rb
Fabricator(:user_role) do
  role 'member'
end
于 2015-05-15T12:38:29.193 に答える