0

What I want to do is get my request unit test to enter the format.text block in my controller:

  def preview
    byebug
    respond_to do |format|
      format.text { render template: 'reporting/iue/report_mailer/report.text' }
      format.html { render template: 'reporting/iue/report_mailer/report.html' }
    end
  end

In my request rspec I'm passing the as: :text option

        subject(:request) do
          get api_iue_preview_path(report.id), headers: auth_headers, as: :text
        end

But it doesn't enter the text block.

I've observed that:

(byebug) request.format
#<Mime::Type:0x007faaf921c8b0 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html", @hash=-3118253865377356622>
(byebug) request.format.html?
true
(byebug) request.format.text?
false

Meaning 'text/html' does not count as text format, but does count as an html.

Does anyone know why?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Leticia Esperon
  • 2,499
  • 1
  • 18
  • 40
  • From what I see the subject should be a hash (`expect(get: 'some_path').to ...`). Try changing it to `{get: api_iue_preview_path(report.id), headers: auth_headers, as: :text}` and see if that works. – 3limin4t0r Aug 15 '19 at 18:06
  • 2
    `text/html` is the correct designator for HTML content. It might look like it means "text or html" but actually it's more like "text, specifically html". The `Content-Type` you actually want is `text/plain`. – John Skiles Skinner Aug 15 '19 at 18:13
  • You could always provide the format option to the path instead. `api_iue_preview_path(report.id, format: :text)` – 3limin4t0r Aug 15 '19 at 18:14
  • @JohnSkilesSkinner thank you. And how do I get a `text/plain` format? `as: :text` didn't do it. – Leticia Esperon Aug 15 '19 at 18:51
  • I'm not sure. Does this work? https://stackoverflow.com/questions/13940840/setting-content-type-header-for-rspec-and-rails-api – John Skiles Skinner Aug 15 '19 at 19:32

0 Answers0