-
Update: repo with reproduction of the error Hi everyone. I'm trying to run this view spec example: # spec/views/expenditures/index.html.erb_spec.rb
require 'rails_helper'
RSpec.describe "expenditures/index" do
include Pagy::Backend
include Pagy::Frontend
it 'displays all current expenditures' do
expenditures = create_list(:expenditure, 2)
expenditures = pagy(expenditures)
assign(:expenditures, expenditures)
assign(:pagy, expenditures)
render
expect(rendered).to match(/Gastos a la fecha/)
end
end but get this error
Notice that I I've tried to stub the allow(expenditures).to receive(:offset) What can I be doing wrong? How can I properly mock Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Looks like If that's the case use the I'll throw in my two cents:
You may even do something like this if you wish: # pagy.rb # config file.
require 'pagy/extras/array' if Rails.env.test? Feel free to re-open the issue if I've missed the mark or you have further questions. |
Beta Was this translation helpful? Give feedback.
-
OK, so there are quite a few things that don't make sense here. The However, you are testing a view, and since you are mocking it rather than running your actual code like in an integration test, you don't need to paginate anything at all with pagy. Pagy is extensively tested with 100% of coverage, so DRY. In order to test your view you have just to RSpec.describe "expenditures/index" do
it 'displays all current expenditures' do
assign(:expenditures, create_list(:expenditure, 2)) # the page array of expenditures
assign(:pagy, Pagy.new(count: total_count_of_your_collection, **other_pagy_vars)
render
expect(rendered).to match(/Gastos a la fecha/)
end
end If you really want to test your actual code (and how you actually use pagy), then you don't need pagy in your tests: you just need to run your code and test the expected output. |
Beta Was this translation helpful? Give feedback.
-
Fixed with code proposed by ddnexus. Thank you all. |
Beta Was this translation helpful? Give feedback.
OK, so there are quite a few things that don't make sense here.
The
pagy*
controller methods accept a collection (which in your case should be an AR scope, not an array) and return an array of 2 items: the pagy object and the paginated collection items, so you are using it improperly. That way theexpenditures
in your test is an array of those 2 items: no wonder it gives errors.However, you are testing a view, and since you are mocking it rather than running your actual code like in an integration test, you don't need to paginate anything at all with pagy. Pagy is extensively tested with 100% of coverage, so DRY.
In order to test your view you have just to
assign
the local variables, so …