は、私は足場FactoryGirl.createは常に
テストはモデルの仕様に渡されるが、それは常にコントローラの仕様に失敗すると、それをユーザーモデル
を作成するので、私がしているスペックコントローラで失敗
私users_controller_spec
require 'rails_helper'
RSpec.describe Api::V1::UsersController, type: :controller do
describe "GET #show" do
before(:each) do
@user = FactoryGirl.create :user
get :show, id: @user.id
end
it { should respond_with 200 }
end
end
私のユーザーズFactoryGirl
FactoryGirl.define do
factory :user do
name "MyString"
sex_id 1
end
end
私は私のコンソールから、私のユーザモデルの仕様
FactoryGirl.create :user
からユーザーを作成することができますが、私はあなたが作成できるようだ私のコントローラの仕様
1) Api::V1::UsersController GET #show
Failure/Error: @user = FactoryGirl.create :user
ActiveRecord::InvalidForeignKey:
PG::ForeignKeyViolation: ERROR: insert or update on table "users" violates foreign key constraint "fk_rails_12f007df76"
DETAIL: Key (sex_id)=(1) is not present in table "sexs".
: INSERT INTO "users"
# ------------------
# --- Caused by: ---
# PG::ForeignKeyViolation:
# ERROR: insert or update on table "users" violates foreign key constraint "fk_rails_12f007df76"
# DETAIL: Key (sex_id)=(1) is not present in table "sexs".
# /home/user/.rvm/gems/ruby-2.1.8/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:18:in `block in initialize'
エラーメッセージに_youはID 1_の性別レコードがありません。 –
ええ私はほとんど同じ属性の2つのモデルを持っていますが、そのモデルはテストに合格しますが、このモデルは失敗します。私のコードは最初から書き直そうとしていますが、この問題は修正されることを願っています – icemelt