Any_instance stub using Minitest

How can I do the following without using any_instance from Mocha? I just want to test a secure controller as described here without using Rspec.

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 
+5
source share
3 answers

You do not need to drown out any instance of PortfoliosController, only an instance that uses this test. This is available in the @controller variable, as described in the 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 
+5
source

I would recommend checking this stone . Lets you do something like ...

 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 

no need to worry about setup .

+2
source

'no Mocha' version of the answer 'blowmage'

  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.stub(:doorkeeper_token,token) do #do your tests end end end 

see http://www.rubydoc.info/gems/minitest/4.2.0/Object:stub

+1
source

Source: https://habr.com/ru/post/1205983/


All Articles