0

I've been working on a Rails 4.1.1 app and I am setting up Rspec. I want to test a controller using the rask-test last_response method. I followed the instructions and here is my spec_helper.rb file

require 'rack/test'
require 'simplecov'
SimpleCov.start

RSpec.configure do |config|
  config.include Rack::Test::Methods
end

and here is my rails_helper file:

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

I have my Rspec Example that does just fine until this line:

r = JSON.parse(last_response)

I get this error message:

Failure/Error: r = JSON.parse(last_response)
     NameError:
       undefined local variable or method `app' for #<RSpec::ExampleGroups::AdminMailActionsController::New:0x007fb100dc5ab0>

Anybody has an idea of what's going on here?

EDIT: Here's the test:

      describe "#new" do
        it "should get the first job in the queue for the Cheat Cheat" do
          @job_queue = FactoryGirl.create(:job_queue)
          @job = FactoryGirl.create(:job, job_queue: @job_queue)
          @organisation = @job_queue.organisation
          @params = {:organisation_id => @organisation.id, :job_queue_id => @job_queue.id}
          get :new, @params
          p last_response.body
          expect(response.status).to eq 200
          expect(assigns(:job_queue)).to eq @job_queue
          expect(assigns(:organisation)).to eq @organisation
          expect(assigns(:job)).to eq @job
        end
    end

response object is huge and the response.body is empty when I should have @mail_action, @job_queue, @organisation and @job available. It works fine in the browser so the variable are there.

EDIT 2: Controller method:

 def new
    @mail_action = MailAction.new
    @mail_action.job_queue = @job_queue
    @job_queue.organisation = @organisation
    if @job_queue.jobs.first.nil?
      @job = Job.new(
        name: 'sample job',
        short_code: 'oomph123',
        job_status_updated_at: DateTime.now,
        created_at: DateTime.now,
        updated_at: DateTime.now,
        job_status: Job::STATUS_ACCEPTED,
        source_file: 'http://my_url.com/test_file.lol',
        complete_file: 'http://my_url.com/awesome_file.lol'
        )
      Job.all.empty? ? @job.id = 1 : @job.id = Job.last.id + 1
      @job.user = current_user
      @job.job_action = JobAction.first
      @job.job_queue = @job_queue
    else
      @job = @job_queue.jobs.first
    end
  end

So if I understood well I should have @mail_action, @job_queue, @organisation and @job available.

  • Firstly: when using simplecov with rails, it's better to use `SimpleCov.start 'rails'` Secondly: rspec is clearly complaining about:`RSpec::ExampleGroups::AdminMailActionsController::New` - is that an example group of yours? if so : can you show it to us please? At the very least - you are trying to parse a response in your rspec test... but not showing us what that actually is... can you please do that? (Note: don't add it as a comment, edit your question and add it there). – Taryn East Sep 23 '14 at 00:35
  • ok - so... it's not necessary to use @vars in your specs... just use regular old variables (without the @). Also: `params` is a magick variable name in rails (and in the rails-related specs too). I'd recommend not using it as a variable name (call it something else). Note: this may not fix your problem: but worth trying first. – Taryn East Sep 23 '14 at 01:06
  • Also: you haven't quite given us enough.... you are describing something... in what context? Are you testing a controller? if so - can you show us what's in the controller? (please include all relevant code). Either somewhere in your real code, there's a variable named `app` that it can't find (and should)... or your specs can't find your actual Rails app... which indicates a setup problem... which means it doesn't matter what's in your specs, there's something deeper that's broken. – Taryn East Sep 23 '14 at 01:08
  • I did add the requested controller method. any hints? Cheers – Bertrand Dubaut Sep 24 '14 at 06:25
  • You still haven't given us all the relevant code. for instance, the error message refers to `r = JSON.parse(last_response)` which I don't see i n your code anywhere... therefore you haven't given us everything that is relevant yet. I don't know what code you need to give me... it has to be everything relevant to your problem. All code that is called or traversed while doing this... – Taryn East Sep 24 '14 at 07:08
  • Secondly: `@job_queue` is set up where in your controller action? who populates it? Also: `@job_queue` is inside your controller action... it is *not* available inside your rspec example (where you must set it up yourself). if you want the one that is inside your controller action, you have to look at `assigns` (go look it up). You should not have any `@` variables in your rspec test - you don't need them AT ALL, and its best to replace them with just ordinary variables... do that now. I'll wait :) – Taryn East Sep 24 '14 at 07:09

0 Answers0