2017-11-22 1 views
2

나는 다음과 같은 테스트 코드가 있습니다사용자 번호의 FIRSTNAME는 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 

이제 오류 메시지가 표시됩니다. 그 사람은 0입니다. 왜? 나는 사용자를 생성하기 전에 그 사람을 저장하고 데이터도 함께 전송됩니다. 필요한 모든 속성은 담당자에게 포함되며 사용자 모델에는 사람 (belongs_to)에 대한 링크가 있습니다. 사람 모델에는 사용자 (has_many)에 대한 링크가 포함되어 있습니다. 팩토리 사용자와 사람 모두 거기에 있으며 데이터로 가득 차 있습니다. 사실, 이것은 문제없이 작동해야하지만 그렇지 않습니다.

내가 설명 할 수없는 이유로 저장 (지속)이 불가능합니다. 여기에 사용자 모델이 있습니다 :

class User < ApplicationRecord 

    # Associations 
    belongs_to :person 

    delegate :firstname, to: :person 
    delegate :surname, to: :person 

    .... 
end 

대리인을 대신 양도 할 사람이 있습니까? 내가 생각

답변

0

, 당신은 작업에 set_person 방법을 추가 할 수 있습니다

# ... 
step Model(User, :create) 
step Policy::Pundit(User::Policy, :create?) 
step Contract::Build() 
step Contract::Validate(representer: User::Representer::In::Default) 
step :set_person 
# ... 

def set_person(options, **) 
    person = Person.find(options['contract.default'].person.id) 
    options['contract.default'].person = person 
end 
+0

아니, 난 안 무서워. 같은 실수. – Daniel