如果不使用摩卡的any_instance,如何执行以下操作?我只想在不使用Rspec的情况下将受保护的Controller测试为described here。
class PortfoliosControllerTest < ActionController::TestCase
setup do
@portfolio = portfolios(:p2)
user = @portfolio.user
token = Doorkeeper::AccessToken.create!(application_id: 'minitest',
resource_owner_id: user.id)
PortfoliosController.any_instance.stubs(:doorkeeper_token).returns(token)
end
end
答案 0 :(得分:5)
您不需要存根任何PortfoliosController实例,只需要测试正在使用的实例。这可以在@controller
变量中找到,如ActionController::TestCase documentation中所述。
class PortfoliosControllerTest < ActionController::TestCase
setup do
@portfolio = portfolios(:p2)
user = @portfolio.user
token = Doorkeeper::AccessToken.create!(application_id: 'minitest',
resource_owner_id: user.id)
@controller.stubs(:doorkeeper_token).returns(token)
end
end
答案 1 :(得分:2)
我建议您查看this gem。允许你做类似的事情......
class PortfoliosControllerTest < ActionController::TestCase
def cool_test
PortfoliosController.stub_any_instance(:doorkeeper_token, token) do
# Assert whatever you were going to assert
end
end
end
很高兴不必担心setup
。
答案 2 :(得分:1)
'没有摩卡'版本的'blowmage'回答
for(int i=0; i<n-1; i++)
for(int j=i+1; j<n; j++)
if(a[j] < a[i])
swap(a[j],a[i]);