ruby on rails - Pass variables in fabricator gem and rspec -
how create user passing variable used transient in rspec? doing wrong?
controller_tests_spec.rb
describe "get index" context "with admin user_role" before(:each) set_current_admin todo_list1 = fabricate(:todo_list) todo_list2 = fabricate(:todo_list) :index end "redirects todo lists path" 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
fabricators
# user_fabricator.rb fabricator(:user) 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) role end
all processed values (including transients) available through attrs hash passed each field fabricated. modified fabricators think expecting.
i defaulted role field in :user_role
"member". way had try expand use fabricator named role
, don't think want.
# user_fabricator.rb fabricator(:user) 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) role 'member' end
Comments
Post a Comment