Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stub any_instance using Minitest

How can I do the following without using any_instance from Mocha? I just want to test a protected 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
like image 540
André Ricardo Avatar asked Oct 31 '14 18:10

André Ricardo


People also ask

How do you stub in Minitest?

Stubbing in Minitest is done by calling . stub on the object/class that you want to stub a method on, with three arguments: the method name, the return value of the stub and a block. The block is basically the scope of the stub, or in other words, the stub will work only in the provided block.

Does rails Minitest?

Minitest is the default testing suite used with Rails, so no further setup is required to get it to work.

What is Minitest mock?

Minitest is a complete testing suite for Ruby, supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.

What is stub instance?

Use any_instance.stub on a class to tell any instance of that class to. return a value (or values) in response to a given message. If no instance. receives the message, nothing happens.


1 Answers

You don't need to stub any instance of PortfoliosController, just the instance that the test is using. This is available in the @controller variable, as explained 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
like image 179
blowmage Avatar answered Oct 07 '22 21:10

blowmage