我正在尝试使用 rspec 为我的 rails 应用程序编写测试,我基本上想创建两个实例
1) 计数器值为 0 的用户
2) 计数器值为 5 或更多的用户
这是我的工厂用户代码
FactoryBot.define do
factory :user do
sequence(:email) { |n| "user#{n}@abc.com" }
name 'rishabh agarwal'
password '12345678'
counter 0
end
end
在我写的 user_controller_spec 文件中
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
虽然我曾经let!
创建实例
let!(:dummyUser){create :user}