С чего бы это не получилось? Как мне исправить тест rspec или мой код?
МОДЕЛЬ
class Coupon < ApplicationRecord
def redeem!
Coupon.increment_counter(:times_redeemed, self.id)
end
end
Rspec
require "rails_helper"
RSpec.describe Coupon, type: :model do
let(:coupon) { create(:coupon) }
describe "#redeem!" do
it "should increment redeemed count and return it" do
expect(coupon.times_redeemed).to eql(0)
coupon.redeem!
expect(coupon.times_redeemed).to eql(1)
end
end
end
Заводская
FactoryBot.define do
factory :coupon do
amount_off { 50 }
stripe_id { "" }
duration { "once" }
duration_in_months { 1 }
max_redemptions { 1 }
metadata { "" }
name { "OFF50" }
percent_off { "50" }
redeem_by { "" }
times_redeemed { 0 }
status_valid { }
end
end
Таблица
create_table "coupons", force: :cascade do |t|
t.string "stripe_id"
t.integer "amount_off"
t.string "duration"
t.integer "duration_in_months"
t.integer "max_redemptions"
t.json "metadata", default: {}, null: false
t.string "name"
t.decimal "percent_off"
t.datetime "redeem_by"
t.integer "times_redeemed"
t.boolean "status_valid", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["stripe_id"], name: "index_coupons_on_stripe_id"
end
Сбои:
1) Coupon#redeem! should increment times_redeemed by 1
Failure/Error: expect(coupon.times_redeemed).to eql(1)
expected: 1
got: 0
(compared using eql?)
Когда я пытаюсь запустить его в консоли, он также не обновляется:
2.6.3 :006 > a
=> #<Coupon id: 2, stripe_id: nil, amount_off: nil, duration: nil, duration_in_months: nil, max_redemptions: nil, metadata: {}, name: nil, percent_off: nil, redeem_by: nil, times_redeemed: 0, status_valid: true, restricted: {}, created_at: "2019-11-02 19:44:07", updated_at: "2019-11-02 19:44:07">
2.6.3 :007 > a.redeem!
Coupon Update All (5.5ms) UPDATE "coupons" SET "times_redeemed" = COALESCE("times_redeemed", 0) + 1 WHERE "coupons"."id" = $1 [["id", 2]]
=> 1
2.6.3 :008 > a
=> #<Coupon id: 2, stripe_id: nil, amount_off: nil, duration: nil, duration_in_months: nil, max_redemptions: nil, metadata: {}, name: nil, percent_off: nil, redeem_by: nil, times_redeemed: 0, status_valid: true, restricted: {}, created_at: "2019-11-02 19:44:07", updated_at: "2019-11-02 19:44:07">
2.6.3 :009 >