我有一个account模型,它有一个trial_ends_at字段,当用户创建帐户时,我希望将它设置为30.days.from_now。
我正在使用RSpec和timecop测试试用期,但一直未能做到这一点。
进展至今
it "sets the trial_ends_at to 30 days from now" do
post admin_accounts_path, params: { account: { name: "My company", subdomain: "example" } }
Timecop.freeze(Time.zone.today + 30) do
expect(Account.first.trial_ends_at).to eq(30.days.from_now)
end
end考试没有通过。
另外,我也尝试过
it "sets the trial_ends_at to 30 days from now" do
post admin_accounts_path, params: { account: { name: "My company", subdomain: "example" } }
Timecop.freeze(Time.zone.today + 30) do
expect(Account.first.on_generic_trial?).to be_truthy
end
end这个测试很脆弱。它代表30.days.from_now,如果试验设置低于该值,则失败,但如果试验设置高于该值,则失败,例如:50.days.from_now。
on_generic_trial?方法来自付钱 gem。
发布于 2021-06-01 17:26:49
您的代码是这样的:创建帐户并设置trial_ends_at = Time.now + 30 days,然后前进到下一个月,然后尝试期望trial_ends_at与下一个月相等,所以失败了。让我们试试:
it "sets the trial_ends_at to 30 days from now" do
Timecop.freeze(Time.zone.today) do
post admin_accounts_path, params: { account: { name: "My company", subdomain: "example" } }
expect(Account.first.trial_ends_at).to eq(30.days.from_now)
end
endhttps://stackoverflow.com/questions/67793019
复制相似问题