我有一个带有以下代码的 ArticleController:
def edit
@article = current_user.articles.find(params[:id])
end
以及以下测试:
describe "GET 'edit'" do
it "should fail when signed in and editing another user article" do
sign_in @user
get :edit, :id => @another_user_article.id
response.should_not be_success
end
end
但是,当我开始测试时,我收到以下错误(这是正常的),但我想知道如何处理以便我的测试可以通过?
Failure/Error: get :edit, :id => @another_user_article.id
Mongoid::Errors::DocumentNotFound:
Document not found for class Article with id(s) 4f9e71be4adfdcc02300001d.
我想过用这个来改变我的控制器方法,但这对我来说似乎不合适:
def edit
@article = Article.first(conditions: { _id: params[:id], user_id: current_user.id })
end