Testing a presenter with MiniTest - ruby-on-rails

We have a presenter class that works great in the application but we cannot figure out how to write a test for it in Minitest because we are passing in the view_context and it seems it is impossible to get that in Minitest.
The class is called like this from our controller.
OrdersPresenter.new(view_context)
How can we get a view_context in a Minitest?

Related

Defining stub methods in test_helper?

Can I define all of my stub methods at one place instead of defining them in each test case?
For example i have this piece of code
SocialNetworks::Facebook.any_instance.stubs(:update_status).returns(true)
in multiple test cases. But if I move it in test_helper.rb file then running test cases won't work.
Does mocha gem allowed to place these stubs at one place?
I am using rails Minitest for testing rails app. And mocha gem for
stubing and mocking.
You can do that in setup callback, like:
class YourTest < ActiveSupport::TestCase
setup do
SocialNetworks::Facebook.any_instance.stubs(:update_status).returns(true)
end
end
setup is called before each test case.

How to stub a Rails helper/module method in Rspec 3 feature spec?

I've seen this done using the older should_receive syntax, but I can't figure out how to stub a Rails module method in Rspec 3.
I've tried these:
OptionsHelper.stubs(:method).returns("something")
allow(OptionsHelper).to receive(:method).and_return "something"
allow_any_instance_of(ActionView::Base).to receive(:render_quick_help).and_return 'something'
But none actually stub the method.
allow_any_instance_of(ActionView::Base)
Means it will expect instance of that exact class and not classes that inherit from it to receive :method.
Also ActionView::Base is a class, ActionView:: is a module.
Furthermore, Modules can not be instantiated, so in theory allow_any_instance_of(ActionView).to ... will have no effect.
I know it's not a solution, but should point you in right direction.

NoMethodError: undefined method `stub' for ModuleX:Module

Using Mocha, I am trying to mock a controller method that calls a module method.
This is for an integration test.
Example:
class Controller < ApplicationController
def method1
response = Module1.method2(...
My steps so far:
Added mocha to gemfile
Added require 'mocha/mini_test' to the very bottom of my
test_helper.rb
Tried this code in my integration test before sending a post to my controller:
Module1.stub(:method2).returns(:true)
post "controller/method1"
And got this error:
NoMethodError: undefined method 'stub' for Module1:Module
Is it possible to stub method2?
EDIT: So the main fix is that the method is 'stubs' not 'stub'. I'm still having trouble mocking this dang Module though.
EDIT: Rails and MiniTest just call the module method even after I've stubbed it. Is it possible that Rails is overwriting my stub?
class Test < ActionDispatch::IntegrationTest
test "test do
Module1.stubs(:method2).returns(:true)
post "controller/method1"
This test leads to error inside method2 bc no parameters were passed in. The test is behaving as if the method was not stubbed.
try .stubs with an s.
The stubnotation is to build a stub you'lle use later on.
Add an s when you stub a method directly on something.
regarding your test you may want a
Module1.expects(:method2)
but the stub should work anyway. Rails would not overrite your stub, maybe you test_helper does, but i'm quite sure it is more a syntax thing.
Paste your real code and test because here right know it's kind of difficult and can be anything... a callback preveting the method to be called, the test syntax, ...
for me this was caused by not adding require 'minitest/mock' to my test_helper.rb

Domain object extraction with MiniTest

I have two questions. The first is a basic one. MiniTest is a drop-in replacement for UnitTest. So if you use generators in rails, you get a bunch of ActiveSupport::TestCase classes. If you use minitest (without minitest-spec-rails) then you can do
1.must_equal 1
# instead of
assert_equal 1, 1
Correct? So it's confusing to me why there's a MiniTest::Unit::TestCase class as described in railscast #327 (pro episode paywall sry). It's confusing when looking at a test suite which I'm actually using. If minitest is a drop-in replacement then I'm using minitest to execute testunit style rails tests. If I take it out, I'm using testunit to run testunit tests?
So let's say I am using MiniTest to run TestUnit style tests.
require 'test_helper'
class FooTest < ActiveSupport::TestCase
end
After watching destroyallsoftware's screencast on extracting domain objects, I was inspired. He makes some good points about avoiding loading test_helper.rb to speed up the test suite without resorting to spork trickery (which is exactly what I do). But how can I avoid loading test_helper.rb when that's what gives me ActiveSupport::TestCase from above?
Can you not extract domain objects and put them in lib/ or extras/ with MiniTest or TestUnit?
The answer to your first question is "no, not correct". Minitest is not a "drop-in replacement" for Test::Unit. Minitest provides a simpler implementation for most of Test::Unit, but not all of it. In Ruby 1.9 the Test::Unit library is built on top of Minitest's TestCase class, and fills the gaps between the two libraries.
Minitest provides two modes for writing tests: the classic TestCase approach, and a separate Spec DSL. Like Test::Unit, the Spec DSL is built on top of Minitest's TestCase. So while 1.9's Test::Unit and Minitest's Spec DSL are both are built on Minitest's TestCase, you can't use the Spec DSL in Test::Unit because its not part of its ancestry.
The answer to your second question is you cannot avoid test_helper.rb if you want to use ActiveSupport::TestCase. You will need to require minitest/autorun and have your test class inherit from MiniTest::Unit::TestCase.

run database with rspec-rails

I am trying to test a controller with RSpec but am having a problem because a function in the controller requires a database.
the line of code in the controller looks something like:
#myallresources = Myrsources.all
where Myresources just inherits from ActiveRecord::Base
however, because there is no database, there is nothing to load and #myallresources is just an empty array, causing the test to fail. Is there a way to connect to a database while running the rspec?
I am very new to RSpec and rails so any help would be very appreciated. Thanks.
You shouldn't use a database connection in your controller specs.
Check the section about database isolation on this page http://rspec.info/rails/writing/controllers.html
Basically you have to mock or stub your ActiveRecord models, as those should be tested separately in the models specs. Here's a simple example using mock_model:
before do
mocks = (1..3).map { mock_model(MyResource) }
MyResource.should_receive(:all).and_return(mocks)
end
Put this inside the same block where reside the describe definition testing for the actions that use MyResource.all.
You can find good explanation of mocks and stubs in following links:
http://relishapp.com/rspec/rspec-rails/v/2-5/docs/mocks/mock-model
http://relishapp.com/rspec/rspec-rails/v/2-5/docs/mocks/stub-model

Resources