Rspec失败:Coupon.increment_counter不增加记录值

问题描述 投票:0回答:1

为什么会失败?如何更正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 > 
ruby-on-rails ruby activerecord rspec ruby-on-rails-5.2
1个回答
0
投票

当调用Coupon.increment_counter(:times_redeemed, self.id)时,对象coupon不知道数据库中所做的更改,您需要重新加载它以获取这些更改:

    it "should increment redeemed count and return it" do
      expect(coupon.times_redeemed).to eql(0)
      coupon.redeem!
      coupon.reload
      expect(coupon.times_redeemed).to eql(1)
    end
热门问题
推荐问题
最新问题