我已经为搜索方法编写了这些测试(它被称为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
他们都通过了,但我觉得中间测试是测试关系,而不是如果提交没有注释的user_id
则会返回什么。谁能为我澄清一下?我对rspec很新。
答案 0 :(得分:0)
我的控制器代码出现了一个愚蠢的错误!
方法是这样的:
def filter
@user = User.find(params[:user_id])
respond_to do |format|
format.html
format.json { render json: @user }
end
end
将其更改为:
def filter
@user = User.find(params[:user_id])
@user_notes = UserNote.find_by_user_id(@user)
end