[英]Rails alias_attribute not working in RSPEC
店內型號:
class Shop < ActiveRecord::Base
alias_attribute :token, :basic_token
# basic_token column is set NOT to be NULL in DB
end
在商店工廠:
FactoryGirl.define do
factory :shop do
basic_token '12313123123123123123123'
...
...
end
end
在RSPEC Shops Controller中:
require 'rails_helper'
RSpec.describe ShopsController, type: :controller do
before do
@shop = create(:shop) # FAILS
=> Failure/Error: @shop = create(:shop)
=> ActiveRecord::StatementInvalid:
=> PG::NotNullViolation: ERROR: null value in column "basic_token" violates not-null constraint
end
describe "" do
........
end
end
Rails控制台測試:
WITH alias_attribute:
(byebug) @shop = build(:shop)
#<Shop remote_id: 1110212666, domain: "abc.myshopify.com", basic_token: nil, created_at: nil, updated_at: nil, ... >
(byebug) @shop.valid?
true
(byebug) @shop.errors.messages
{}
WITH alias_attribute commented (# alias_attribute :token, :basic_token):
(byebug) @shop = build(:shop)
#<Shop remote_id: 1110212666, domain: "abc.myshopify.com", basic_token: "12313123123123123123123", created_at: nil, updated_at: nil, ... >
(byebug) @shop.valid?
true
(byebug) @shop.errors.messages
{}
評論alias_attribute :token, :basic_token
,它可以工作。
知道為什么我不能使用alias_attribute
進行記錄嗎?
請注意,這在開發中有效 生產環境中,只會在測試中出錯。
謝謝。
在只有一個屬性,一個別名和一個具有相同默認值的工廠的基本模型上,我沒有這種行為(使用RSpec,Factory Girl,Rails 5.1)。 提供工廠的其余部分和模型將有所幫助。
要查找錯誤的來源,您可以:
alias_attribute
以外的整個Shop模型 basic_token
之外的整個工廠 create(:shop)
的虛擬測試通過了 如果以上步驟3失敗:5.嘗試更改別名的名稱,請查看您的應用程序中是否有任何與名稱token
沖突的內容。6.嘗試git bisect
,以查找有罪的提交。 希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.