将标头附加到 Rspec 控制器测试 [英] Appending headers to Rspec controller tests
问题描述
我正在尝试为我的控制器编写测试,该控制器接收来自外部服务的请求.到目前为止,这是我的测试:
describe ApplyController do上下文有效时"做让(:参数)做file = File.join File.dirname(__FILE__), '..', 'samples', 'Indeed.json'JSON.parse(File.read 文件)结尾让(:签名){'GC02UVj0d4bqa5peNFHdPQAZ2BI='}主题(:响应){ 发布:确实,参数,'X-Indeed-Signature' =>签名 }它'如果请求有效则返回 200 ok' 做期望(响应.状态).到 eq 200结尾结尾结尾
我的控制器现在看起来像这样:
class ApplyController <应用控制器确实是定义绑定.撬结尾结尾
当我在测试中进入 Pry 并尝试检查 request.headers['X-Indeed-Signature']
的值时,我总是得到 nil
有什么我遗漏的吗?我正在使用 Rails 3.2 和 Rspec 3
我能够通过使用 @request.env
而不是 @request.headers
来修复它所以:
describe ApplyController do上下文有效时"做让(:参数)做file = File.join File.dirname(__FILE__), '..', 'samples', 'Indeed.json'JSON.parse(File.read 文件)结尾让(:签名){'GC02UVj0d4bqa5peNFHdPQAZ2BI='}它'如果请求有效则返回 200 ok' 做@request.env['X-Indeed-Signature'] = 签名帖子:确实,参数期望(响应.状态).到 eq 200结尾结尾结尾
I'm trying to write out tests for a controller of mine that takes in requests from external services. So far this is my test:
describe ApplyController do
context 'when valid' do
let(:parameters) do
file = File.join File.dirname(__FILE__), '..', 'samples', 'Indeed.json'
JSON.parse(File.read file)
end
let(:signature) { 'GC02UVj0d4bqa5peNFHdPQAZ2BI=' }
subject(:response) { post :indeed, parameters, 'X-Indeed-Signature' => signature }
it 'returns 200 ok if Request is valid' do
expect(response.status).to eq 200
end
end
end
This should work according to the documentation I could find.
My controller right now looks something like this:
class ApplyController < Application Controller
def indeed
binding.pry
end
end
When I get into Pry in my test and try to check the value of request.headers['X-Indeed-Signature']
I always just get nil
Is there something that I am missing? I am using Rails 3.2 and Rspec 3
I was able to fix it by using @request.env
instead of @request.headers
like so:
describe ApplyController do
context 'when valid' do
let(:parameters) do
file = File.join File.dirname(__FILE__), '..', 'samples', 'Indeed.json'
JSON.parse(File.read file)
end
let(:signature) { 'GC02UVj0d4bqa5peNFHdPQAZ2BI=' }
it 'returns 200 ok if Request is valid' do
@request.env['X-Indeed-Signature'] = signature
post :indeed, parameters
expect(response.status).to eq 200
end
end
end
这篇关于将标头附加到 Rspec 控制器测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!