0

I have a simple method to test my index action in one of my Rails controllers. In minitest, it sends a request to GET /{resource} like get :index, format: :json.

In my controller action for index I have a few lines that puts some_var. How can I view this output from the controller action when running the test from the command line?

It is output when sending a request to the server, but doesn't appear when running the test from the command line.

Noah
  • 390
  • 1
  • 3
  • 15

1 Answers1

0

You want: assert_output

controller:

class FooController < ApplicationController
  def bar
    puts 'hello'
  end
end

test:

require 'test_helper'

class FooControllerTest < ActionController::TestCase
  test 'outputs hello' do
    assert_output(/hello/) do # use regex because "hello" will fail per http://stackoverflow.com/a/26855137/46076
      post :bar
    end
  end
end
David Silva Smith
  • 11,498
  • 11
  • 67
  • 91
  • This solution does not work. The output from _within_ the controller is not captured in that assertion method. – G. Shand Dec 21 '21 at 14:50