I am trying to write a test for my rails application using rspec, I basically want to create two instances
1)a user with counter value 0
2)a user with counter value 5 or more
Here is my factory user code
FactoryBot.define do
factory :user do
sequence(:email) { |n| "user#{n}@abc.com" }
name 'rishabh agarwal'
password '12345678'
counter 0
end
end
In user_controller_spec file I have written
context 'get#redeem' do
it 'should not redeem the account for points lesser than 5' do
get :redeem, format: :json,params:{:id=>dummyUser.id}
expect(JSON.parse(response.body)["message"]).to eq("You cannot redeem your points")
end
it 'should redeem the account if points are greater than or equal to 5' do
get :redeem, format: :json,params:{:id=>dummyUser.id}
json=JSON.parse(response.body)
expect(JSON.parse(response.body)["counter"]).to eq(5)
end
end
While I have used let!
to create instance
let!(:dummyUser){create :user}
Found a better way .... Just initialize the counter variable with 0 in factory and while using context for more then 5 change the value of counter dummyUser.counter=5 dummyUser.save this will change the value to 5 and which can be used by the test case.