我一直在开发 Rails 4.1.1 应用程序,并且正在设置 Rspec。我想使用 rask-testlast_response
方法测试控制器。我按照说明进行操作,这是我的spec_helper.rb
文件
require 'rack/test'
require 'simplecov'
SimpleCov.start
RSpec.configure do |config|
config.include Rack::Test::Methods
end
这是我的 rails_helper 文件:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
我有我的 Rspec 示例,在这一行之前做得很好:
r = JSON.parse(last_response)
我收到此错误消息:
Failure/Error: r = JSON.parse(last_response)
NameError:
undefined local variable or method `app' for #<RSpec::ExampleGroups::AdminMailActionsController::New:0x007fb100dc5ab0>
有人知道这里发生了什么吗?
编辑:这是测试:
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 对象很大,当我应该有,@mail_action
和可用的时候 response.body 是空的。它在浏览器中运行良好,因此变量在那里。@job_queue
@organisation
@job
编辑2:控制器方法:
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
因此,如果我理解得很好,我应该有@mail_action
,和可用的。@job_queue
@organisation
@job