我有一个PagesController,有一个机器人方法。
class PagesController < ApplicationController
...
def robots
respond_to :text
expires_in 6.hours, public: true
end
...
end
匹配视图:robots.text.erb
<% if !ENVied.BLOCK_ROBOTS %>
User-agent: *
Disallow:
<% else %>
User-agent: *
Disallow: /
<% end %>
当手动测试时,这按预期工作,但是,我在编写测试时遇到了一些麻烦。
RSpec.describe PagesController, type: :controller do
...
describe "Staging, Test and Development environments" do
it "should generate a robots.txt that disallows access to everything." do
ENV['BLOCK_ROBOTS']='true'
get :robots, params: {format: "text"}
expect(response.header['Content-Type']).to include 'text/plain'
expect(response.body).to eq("User-Agent: * Disallow: /")
end
end
...
end
预期输出应为:
User-agent: *
Disallow:
但是,返回的错误是:
Failure/Error: expect(response.body).to eq("User-Agent: * Disallow: /")
expected: "User-Agent: * Disallow: /"
got: ""
(compared using ==)
注释掉第二个期望语句,并且只有expect(response.header…
语句通过。
手动导航到/robots.txt 正在产生所需的结果,所以我相信我的第二个期望语句有问题。
任何指导将不胜感激。
导轨 5红宝石 2.3.1
gem 'rspec-rails', '3.5.2'
我相信你应该能够使用任何一个
request.accept = "text/plain"
在调用get :robots
或更改指定格式的方式之前:
get :robots, { format: :text }