Reputation: 325
I'm trying to fake a response to a method call, but without success.
In my tests, the html rendered page will show the number of pages from. call to <%= @pagy.pages %>
(I'm using Pagy gem), but in my test I dont need to count the pages, only to check if the page was opened.
So, the @pagy was never initialized in the test.
I need Minitest to "fake" an answer to @pagy.pages
to always respond the number 1
:
How to do this?
Thanks!
After the answer from @Ahmed, this is the new code, but still not working:
mock = MiniTest::Mock.new
mock.expect :pages, 1
Pagy.stub :new, mock do
create :lando, :brazilo
get '/ameriko/brazilo'
assert_response :success
end
The view still cannot find @pagy.pages
method.
Upvotes: 0
Views: 428
Reputation: 558
You can use Minitest Mocks in setup
block (also can work if you put this code inside the test case block itself):
mock = Minitest::Mock.new
mock.expect :pages, 1
Pagy.stub :new, mock do
# Put the code that relies on `@pagy.pages` is `1`
end
Backtrace:
Minitest::UnexpectedError: ActionView::Template::Error: undefined method `pages' for nil:NilClass
app/views/events/_events_as_cards.haml:11:in `_app_views_events__events_as_cards_haml__846217806806687455_70361502341700'
app/helpers/events_helper.rb:27:in `display_events_by_style'
app/views/events/by_country.haml:20:in `_app_views_events_by_country_haml__4566190630208235120_70361501243420'
test/controllers/events_controller_test.rb:41:in `block (2 levels) in <class:EventsControllerTest>'
test/controllers/events_controller_test.rb:39:in `block in <class:EventsControllerTest>'
app/views/events/_events_as_cards.haml:11:in `_app_views_events__events_as_cards_haml__846217806806687455_70361502341700'
app/helpers/events_helper.rb:27:in `display_events_by_style'
app/views/events/by_country.haml:20:in `_app_views_events_by_country_haml__4566190630208235120_70361501243420'
test/controllers/events_controller_test.rb:41:in `block (2 levels) in <class:EventsControllerTest>'
test/controllers/events_controller_test.rb:39:in `block in <class:EventsControllerTest>'
Finished in 1.53247s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
I, [2019-07-15T10:00:50.384204 #47531] INFO -- : Running locally, skipping Codacy coverage
Process finished with exit code 1
Upvotes: 1