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.