模型中的方法被RSpec测试忽略。

huangapple go评论44阅读模式
英文:

Method in model is ignored by test rspec rails

问题

我有一个在我的模型中的方法,如果多个项目具有相同的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

测试输出:

Failure/Error: expect(shopping_cart.reload.items.count).to eq(1)

   expected: 1
        got: 2
英文:

I have a method in my model that group items by variant id if multiple items have the same variant_id she merge them and add their quantity.

My model:

class ShoppingCart &lt; ApplicationRecord
  belongs_to :company
  belongs_to :user
  has_many :items, class_name: &quot;ShoppingCartItem&quot;, dependent: :destroy
  accepts_nested_attributes_for :items, reject_if: proc { |attributes| attributes[&#39;quantity&#39;].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

This method works well when i try it manually but in my tests rspec seems to ignore this method

My tests:

require &#39;rails_helper&#39;

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

  describe &quot;merge_items&quot; do
    let(:shopping_cart) { create(:shopping_cart) }

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

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

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

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

Tests output:

Failure/Error: expect(shopping_cart.reload.items.count).to eq(1)

   expected: 1
        got: 2

答案1

得分: 0

我改变了代码,现在它可以正常工作:

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

请注意,我已将代码中的HTML实体字符(如&quot;)替换为双引号(")以便更好地阅读。如果您需要其他帮助,请随时提问。

英文:

I changed the code and this works:

require &#39;rails_helper&#39;

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

  describe &quot;merge_items&quot; do
    let(:shopping_cart) { create(:shopping_cart) }


    context &quot;same variant_id&quot; do
      let(:params) {{ shopping_cart: {items_attributes: [{variant_id: &#39;same variant_id&#39;, quantity: 2}, {variant_id: &#39;same variant_id&#39;, quantity: 2}]}}}

      before do
        shopping_cart.update params[:shopping_cart]
      end

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

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

    context &quot;not same variant_id&quot; do
      let(:params) {{ shopping_cart: {items_attributes: [{variant_id: &#39;variant_id&#39;, quantity: 2}, {variant_id: &#39;different variant_id&#39;, quantity: 2}]}}}

      before do
        shopping_cart.update params[:shopping_cart]
      end

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

huangapple
  • 本文由 发表于 2023年2月14日 18:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定