user1502223
user1502223

Reputation: 645

How to test this Rspec Ajax request

I'm trying to test the scenarios method in my show action

def scenarios
  @scenarios ||= fund.present? ? fund.scenarios : []
end

def fund
  @fund ||= funds.find_by(id: fund_id)
end

def fund_id
  return nil unless params.fetch(:report, false)
  params.fetch(:report).fetch(:fund_id, 0)
end

I feel like i need to get the ajax request and grab the params of fund_id out of it but I am unsure how to do that or if that is what I am suppose to do

this is my my ajax request in my js file

$('#geodistributions-filter-form-container')
  .on('change', 'select#report_fund_id', function() {
  $.get('/reports/geodistribution.js',                            
  $(this).parents('form').serialize());
});

I want to test two contexts

context 'scenario when a fund is not present'

context 'scenario when a fund is present'

I am unsure how to start off testing this if someone can point me in the right direction that would be great.

Upvotes: 0

Views: 159

Answers (1)

Uzbekjon
Uzbekjon

Reputation: 11813

Well, if I understood you correctly, you want to test if your ajax calls. You can use acceptance tests frameworks like Capybara or simply test your call to /reports/geodistribution.js URL just like any other controller call. Setup the required environment (with Fund, without Fund) and check it behaves correctly.

I would also refactor your code a bit:

def scenarios
  @scenarios ||= fund.try(:scenarios) : []
end

def fund
  # if there is no fund found, let it throw an exception
  @fund ||= Funds.find(params[:report][:fund_id])
end

Upvotes: 1

Related Questions