Possible Duplicate:
Is it possible to specify a user agent in a rails integration test or spec?




我正在使用rspec在Rails应用程序中测试请求。我需要能够在请求之前设置用户代理。

这不起作用:

  describe "GET /articles feed for feedburner" do
it "displays article feed if useragent is feedburner" do
  # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
  @articles=[]
  5.times do
    @articles << Factory(:article, :status=>1, :created_at=>3.days.ago)
  end
  request.env['HTTP_USER_AGENT'] = 'feedburner'
  get "/news.xml"
  response.should be_success
  response.content_type.should eq("application/xml")
  response.should include("item[title='#{@articles.first.title}']")
end


结束

如何正确指定用户代理?

最佳答案

尝试在测试中使用它:

request.stub!(:user_agent).and_return('FeedBurner/1.0')


或对于较新的RSpec:

allow(request).to receive(:user_agent).and_return("FeedBurner/1.0")


FeedBurner/1.0替换为要使用的用户代理。我不知道确切的代码是否可以工作,但是something like it should

10-07 21:16