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
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.
Minitest is the default testing suite used with Rails, so no further setup is required to get it to work.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With