[英]Expected Count to change by 1 but was changed by 0 - Pry within Rspec
因此,我有以下模型:ExistingCustomer和所有測試都很好,並且運行良好。 客戶請求了幾個新字段。 一個是一個簡單的字符串,所以我進行了更新。 另一個是與特定區域的關聯。 更新了MVC,它工作正常。 它存儲等
但是我的rspec創建失敗,並且我無法終生弄清楚為什么/如何找到錯誤。
所以我的規格看起來像:
require 'rails_helper'
require 'pry'
RSpec.describe ExistingCustomersController, type: :controller do
describe 'create' do
it 'should create existing customer' do
binding.pry
expect do
binding.pry
post :create, params: { existing_customer: FactoryBot.attributes_for(:existing_customer) }
end.to change(ExistingCustomer, :count).by(1)
end
it 'sends an email' do
expect(ExistingCustomerMailer).to send_mail(:existing_customer_request)
post :create, params: { existing_customer: FactoryBot.attributes_for(:existing_customer) }
end
end
end
FactoryBot看起來像:
FactoryBot.define do
factory :existing_customer do
region
first_name { 'Jake' }
last_name { 'Peralta' }
phone { '991-999-9999' }
email { 'best.detective@ninenine.com' }
title { 'World\'s Best Detective/Genius' }
account_number { 99 }
company_name { 'NYPD' }
existing_customer_email { 'better.detective@ninenine.com' }
end
end
我已經完成了association:region和just region的工作。 沒有不同。 代替Factory我做了類似的事情:
let(:existing_params)做{first_name:'Jake',依此類推}
然后通過那個代替FactoryBot。 同樣的問題。
在所有情況下,它都無法創建。 我如何找出造成Pry問題的原因。 我知道要在it塊中插入binding.pry進行訪問。 我應該在撬動中抓住什么進行測試? 關於rspec中的撬動測試,我是一個新手。
編輯:所以我在控制器內放置一個binding.pry在創建:
def create
@existing_customer = ExistingCustomer.new(existing_customer_params)
binding.pry
if @existing_customer.save
redirect_to existing_customer_success_path
ExistingCustomerMailer.existing_customer_request(@existing_customer).deliver
else
render 'new'
end
結束
再次運行測試,並執行@ existing_customers.errors。 返回@messages =:region => {必須存在}
因此,問題是如何在規范的參數中傳遞特定的關聯ID?
問題是attributes_for和與FactoryBot的關聯。 這使測試通過:
it 'should create existing customer' do
expect do
post :create, params: { existing_customer: FactoryBot.attributes_for(:existing_customer)
.merge(region_id: create(:region)) }
end.to change(ExistingCustomer, :count).by(1)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.