0

コントローラーに次のようなメソッドがあります。

def resend_confirmation
  @current_user.send_confirmation_instructions

  render json: nil, status: 200
end

そのメソッドの次の仕様を書きました。

require 'rails_helper'

describe 'POST /api/v1/users/:id/resend_confirmation' do
  let!(:current_user) { create(:user) }

  before do
    expect(current_user).to receive(:send_confirmation_instructions)

    post resend_confirmation_api_v1_user_path(current_user),
         headers: http_authorization_header(current_user)
  end

  describe 'response' do
    it 'is empty' do
      expect(response.body).to eq 'null'
    end
  end

  it 'returns 200 http status code' do
    expect(response.status).to eq 200
  end
end

しかし問題は、この仕様が通用しないことです。この行は失敗しています:

expect(current_user).to receive(:send_confirmation_instructions)

それを変更すると

expect_any_instance_of(User).to receive(:send_confirmation_instructions)

すべてがうまく機能します。期待構文を使用した仕様が渡されない理由を誰かに説明してもらえますか?

編集:

これはエラーがどのように見えるかです:

Failures:

  1) POST /api/v1/users/:id/resend_confirmation returns 200 http status code
     Failure/Error: expect(current_user).to receive(:send_confirmation_instructions)

       (#<User id: 4175, email: "test1@user.com", date_of_birth: "1990-01-01", created_at: "2016-07-18 06:56:52", updated_at: "2016-07-18 06:56:52", sex: "male", touch_id_enabled: false, first_name: "Test", last_name: "User", athena_health_patient_id: nil, photo_url: nil, admin: false, one_signal_player_id: "1", phone_number: nil, state: nil, address: nil, city: nil, zip_code: nil, phone_number_confirmed: false>).send_confirmation_instructions(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments
     # ./spec/requests/api/v1/user/resend_confirmation_spec.rb:7:in `block (2 levels) in <top (required)>'

  2) POST /api/v1/users/:id/resend_confirmation response is empty
     Failure/Error: expect(current_user).to receive(:send_confirmation_instructions)

       (#<User id: 4176, email: "test2@user.com", date_of_birth: "1990-01-01", created_at: "2016-07-18 06:56:53", updated_at: "2016-07-18 06:56:53", sex: "male", touch_id_enabled: false, first_name: "Test", last_name: "User", athena_health_patient_id: nil, photo_url: nil, admin: false, one_signal_player_id: "2", phone_number: nil, state: nil, address: nil, city: nil, zip_code: nil, phone_number_confirmed: false>).send_confirmation_instructions(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments
     # ./spec/requests/api/v1/user/resend_confirmation_spec.rb:7:in `block (2 levels) in <top (required)>'
4

1 に答える 1