英文:
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 < 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
This method works well when i try it manually but in my tests rspec seems to ignore this method
My tests:
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
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实体字符(如"
)替换为双引号(")以便更好地阅读。如果您需要其他帮助,请随时提问。
英文:
I changed the code and this works:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论