当我测试我的控制器时,我有这种代码:
describe "GET 'show'" do
let!(:comment) { build_stubbed(:comment) }
before(:each) { Project.stubs(:find).returns(project) }
it "render show template" do
get 'show', id: project.id
should render_template('show')
end
it "create a new comment" do
Comment.expects(:new).returns(comment)
get 'show', id: project.id
end
end
我的问题如下:我必须传递一个ID才能使用方法load_and_authorize_resourceàoruede la ligne 139 dans ce code: https://github.com/ryanb/cancan/blob/2.0/lib/cancan/controller_resource.rb检查参数中是否有ID。
如何解决这个问题?
谢谢!
编辑: 换句话说:变量@project不是由load_and_authorize_resource方法实现的,因为没有ID并且可以检查是否有id来查找模型。
答案 0 :(得分:0)
我不太清楚我明白你在问什么,但你可以这样做:
before(:each) { Project.stubs(:find).with(anything).returns(project)
将任何带有单个参数的Project.find
调用存根,无论该参数是什么。这有帮助吗?