質問

I've written these tests for a search method (it's called filter):

context 'GET #filter' do
  login_admin
  let!(:note) { create(:user_note) }
  let!(:user_with_note) { create(:user, user_notes: [note]) }
  let!(:user_without_note) { create(:user) }

  specify 'returns an array of user filtered notes' do
    get :filter, user_id: user_with_note
    expect(assigns(:user_notes)).to eq [note]
  end

  # This tests the relationship and not the action, right?
  specify 'returns nothing for user without notes' do
    get :filter, user_id: user_without_note
    expect(user_without_note.user_notes).to eq []
  end

  specify 'renders the :filter view' do
    get :filter, user_id: user_with_note
    expect(response).to render_template :filter
  end
end

They all pass fine but I get the feeling that the middle test is testing the relationship and not what is returned if a user_id with no notes is submitted. Can anyone clarify that for me? I'm very new to rspec.

役に立ちましたか?

解決

There was a silly error in my controller code!

Method was this:

def filter
  @user = User.find(params[:user_id])
  respond_to do |format|
    format.html
    format.json { render json: @user }
  end
end

Changed it to this:

def filter
  @user = User.find(params[:user_id])
  @user_notes = UserNote.find_by_user_id(@user)
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top