【发布时间】:2014-02-24 22:41:14
【问题描述】:
我已经使用 RSpec 和 Cucumber 几个月了。但由于我是这里唯一的开发人员,所以都是自学,所以我要求澄清在哪里测试什么。
我目前正在为Coupons 创建一个 CMS。有一个创建新优惠券的表格。我有在 Cucumber 中工作和测试的快乐之路。我是否也应该对表格填写错误进行测试?如果是这样,我是否应该为每个未通过验证的案例创建一个场景?
我已经在 RSpec 中测试了我的验证:
it { should validate_presence_of(:name) }
it { should validate_presence_of(:code) }
describe "validations" do
specify "the start date must be before the end date" do
site_wide_coupon = SiteWideCoupon.new(name: "Free Shipping", code: "ABC123")
site_wide_coupon.start_date = 1.month.from_now
site_wide_coupon.end_date = 1.month.ago
expect(site_wide_coupon.valid?).to be_false
expect(site_wide_coupon.errors.full_messages).to include("Start date must be before the end date")
end
context "it is a flat amount coupon" do
let(:site_wide_coupon) {
site_wide_coupon = SiteWideCoupon.new(name: "Flat Amount", code: "ABC123")
site_wide_coupon.start_date = 1.month.ago
site_wide_coupon.end_date = 1.month.from_now
site_wide_coupon.valid?
site_wide_coupon
}
it "validates presence of discount_amount" do
expect(site_wide_coupon.errors.full_messages).to include("Discount amount can't be blank")
end
it "doesn't validate the presence of discount_percentage" do
expect(site_wide_coupon.errors.full_messages).not_to include("Discount percentage can't be blank")
end
end
context "it is a percentage amount coupon" do
let(:site_wide_coupon) {
site_wide_coupon = SiteWideCoupon.new(name: "Percentage Amount", code: "ABC123")
site_wide_coupon.start_date = 1.month.ago
site_wide_coupon.end_date = 1.month.from_now
site_wide_coupon.valid?
site_wide_coupon
}
it "validates presence of discount_amount" do
expect(site_wide_coupon.errors.full_messages).to include("Discount percentage can't be blank")
end
it "doesn't validate the presence of discount_percentage" do
expect(site_wide_coupon.errors.full_messages).not_to include("Discount amount can't be blank")
end
end
end
describe "#flat_amount?" do
context "name equals 'Flat Amount'" do
it "returns true" do
site_wide_coupon = SiteWideCoupon.new(name: "Flat Amount")
expect(site_wide_coupon.flat_amount?).to be_true
end
end
context "name doesn't equal 'Flat Amount'" do
it "returns false" do
site_wide_coupon = SiteWideCoupon.new(name: "Something else")
expect(site_wide_coupon.flat_amount?).to be_false
end
end
end
describe "#percentage_amount?" do
context "name equals 'Percentage Amount'" do
it "returns true" do
site_wide_coupon = SiteWideCoupon.new(name: "Percentage Amount")
expect(site_wide_coupon.flat_amount?).to be_true
end
end
context "name doesn't equal 'Flat Amount'" do
it "returns false" do
site_wide_coupon = SiteWideCoupon.new(name: "Something else")
expect(site_wide_coupon.flat_amount?).to be_false
end
end
end
那么是否有必要测试我的验证是否在 Cucumber 中触发?还是只是重复我的 RSpec 测试而不增加任何价值?
或者也许我应该只进行一项测试来提交表单而不填写任何内容并测试错误是否显示在页面上?
你们平时都做什么?
【问题讨论】:
标签: ruby-on-rails rspec cucumber