【发布时间】:2015-05-15 01:31:30
【问题描述】:
如何通过在 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
【问题讨论】:
-
您能先告诉我们您观察到的与您期望的不同吗?即你怎么知道它“出了问题”(因为你还没有告诉我们),你希望发生什么?
标签: ruby-on-rails rspec fabrication-gem