2017-04-10 16 views
0

私は、ロボットメソッドを持つPagesControllerを持っています。Rails Rspec予期しない応答テキスト - 一致しない

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に移動することで目的の結果が得られますので、2番目のexpect文に問題があると思います。

ガイダンスをいただければ幸いです。

Railsの5 ルビー2.3.1

gem 'rspec-rails', '3.5.2' 

答えて

0

私はあなたがget :robotsを呼び出す前に

request.accept = "text/plain" 

のいずれかを使用するか、フォーマットを指定する方法を変更することができるはずと信じて:

get :robots, { format: :text } 
関連する問題