ruby-on-rails 模型中的方法被测试rspec rails忽略

wb1gzix0  于 2023-02-17  发布在  Ruby
关注(0)|答案(1)|浏览(240)

我的模型中有一个方法,如果多个项目具有相同的variant_id,则按variant_id对项目进行分组,然后合并它们并添加它们的数量。
我的模特:

class ShoppingCart < ApplicationRecord
  belongs_to :company
  belongs_to :user
  has_many :items, class_name: "ShoppingCartItem", dependent: :destroy
  accepts_nested_attributes_for :items, reject_if: proc { |attributes| attributes['quantity'].blank? }

  before_validation :remove_invalid_items
  before_validation :merge_items

  def merge_items
    self.items = items.group_by { |i| i[:variant_id] }.map do |variant_id, is|
      quantity_sum = is.sum { |i| i[:quantity] }
      ShoppingCartItem.new(variant_id: variant_id, quantity: quantity_sum)
    end
  end
end

当我手动尝试时,这个方法工作得很好,但是在我的测试中,rspec似乎忽略了这个方法
我的测试:

require 'rails_helper'

RSpec.describe ShoppingCart, type: :model do
  describe "associations" do
    it { is_expected.to belong_to(:company) }
    it { is_expected.to belong_to(:user) }
    it { is_expected.to have_many(:items) }
  end

  describe "merge_items" do
    let(:shopping_cart) { create(:shopping_cart) }

    it "merge items if same variant_id" do
        existing_item = create(:shopping_cart_item, shopping_cart: shopping_cart, variant_id: "same variant_id", quantity: 1)
        item = create(:shopping_cart_item, shopping_cart: shopping_cart, variant_id: "same variant_id", quantity: 1)

        expect(shopping_cart.reload.items.count).to eq(1)
    end

    it "not merge items if variant_id different" do
      existing_item = create(:shopping_cart_item, shopping_cart: shopping_cart, variant_id: "variant_id", quantity: 1)
      item = create(:shopping_cart_item, shopping_cart: shopping_cart, variant_id: "different variant_id", quantity: 1)

      expect(shopping_cart.reload.items.count).to eq(2)
    end
  end
end

测试输出:
失效/错误:expect(购物车.重载.商品.计数). to eq(1)

expected: 1
        got: 2
2izufjch

2izufjch1#

我修改了代码,这个工作:

require 'rails_helper'

RSpec.describe ShoppingCart, type: :model do
  describe "associations" do
    it { is_expected.to belong_to(:company) }
    it { is_expected.to belong_to(:user) }
    it { is_expected.to have_many(:items) }
  end

  describe "merge_items" do
    let(:shopping_cart) { create(:shopping_cart) }

    context "same variant_id" do
      let(:params) {{ shopping_cart: {items_attributes: [{variant_id: 'same variant_id', quantity: 2}, {variant_id: 'same variant_id', quantity: 2}]}}}

      before do
        shopping_cart.update params[:shopping_cart]
      end

      it "create just one item" do
        expect(shopping_cart.reload.items.count).to eq(1)
      end

      it "adds all quantities" do
        expect(shopping_cart.reload.items.last.quantity).to eq(4)
      end
    end

    context "not same variant_id" do
      let(:params) {{ shopping_cart: {items_attributes: [{variant_id: 'variant_id', quantity: 2}, {variant_id: 'different variant_id', quantity: 2}]}}}

      before do
        shopping_cart.update params[:shopping_cart]
      end

      it "not merge items if variant_id different" do
        expect(shopping_cart.reload.items.count).to eq(2)
      end
    end
  end
end

相关问题