2
I次のテストコードがあります。ユーザ#のファーストネームはperson.firstnameする委任が、人は皆無である
...
let(:person) { create(:person) }
...
it 'test the create route' do
json_data = '{"data":{"attributes":{"email":"[email protected]","position":""},
"relationships": {"person":{"data":{"id":' + person.id.to_s + ',"type":"people"}}},
"type":"users"}}'
post 'create', body: json_data, format: :json
json = JSON.parse(response.body)
expect(response).to have_http_status(:success)
end
このコードのテストをコントローラクラスメソッド:
public def create
action(User::Create)
.otherwise('user_create_error', 401)
.then_render(User::Representer::Out::Default)
end
アクションメソッド:
public def action(op = nil, effective_params = params)
return if op.nil?
# Call the operation
result = op.(effective_params,
'current_user' => current_user,
'document' => request.raw_post)
# Build and return the OperationResult object
Webapp::OperationResult.new(result, method(:render))
end
op
は、コマンド(トレイルブレイザー操作)を実行する操作です。
class User::Create < Webapp::Operation
contract User::Contract::Default
step Model(User, :create)
step Policy::Pundit(User::Policy, :create?)
step Contract::Build()
step Contract::Validate(representer: User::Representer::In::Default)
step Contract::Persist()
end
今、私はその人がゼロであるというエラーを受け取ります。どうして?私はユーザーを作成する前に人を保存し、データもすべて送信されます。必要なすべての属性は代表者に含まれ、ユーザーモデルにはその人(belongs_to)へのリンクもあります。人物モデルには、ユーザーへのリンク(has_many)も含まれています。ファクトリーのユーザーと人物もそこにあり、データで満たされています。実際には、これは問題なく動作するはずですが、そうではありません。
説明できない理由から、保存(Persist)はできません。
class User < ApplicationRecord
# Associations
belongs_to :person
delegate :firstname, to: :person
delegate :surname, to: :person
....
end
デリゲートには何か他のものがありますか?
いいえ、私は恐れていませんよ。同じ間違い。 – Daniel