1

I'm getting something really weird when I execute my tests with rails test.

I'm trying to understand and create my first controller tests and I get something unexpected.

I get this error in the console:

F

Failure:
FeedbacksControllerTest#test_should_create_resource [/Users/Daniel/GitHub/haeapua-rails/test/controllers/feedbacks_controller_test.rb:15]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/>
Response body: <html><body>You are being <a href="http://www.example.com/">redirected</a>.</body></html>

here is my test

test "should create resource" do

  assert_difference 'Feedback.count', 1 do 
    post feedbacks_url, params: { feedback: { email: "me@myemail.com", summary: "my feedback", rating: 5 } }
  end

  assert_response :success
  assert_redirected_to root_url
end

here is my controller

class FeedbacksController < ApplicationController

  # GET /feedbacks/new
  def new
    @feedback = Feedback.new
    # @feedback.user = current_user if user_signed_in?
  end

  # POST /feedbacks
  def create
    @feedback = Feedback.new(feedback_params)
    # @feedback.user = current_user if user_signed_in?

    if @feedback.save
      # flash[:info] = "We got it, thanks. Someone will contact you ASAP once we read it"
      redirect_to root_url
    else
      render :new
    end
  end

  private
    def feedback_params
      params.require(:feedback).permit(:email, :summary, :rating)
    end
end

When I put a "puts root_url" in my feedback controller just before the redirect_to I get the value "http://www.example.com/". I search for root_url in my whole code and the only part I have it is in the Controller and in my routes root to: 'static_pages#concept'

I'm using devise, do you think it could be because of that? I'm a bit lost and don't know where to start looking!

Daniel Costa
  • 275
  • 2
  • 14

2 Answers2

0

Maybe some default config is setting this value. https://github.com/teamcapybara/capybara/blob/master/lib/capybara.rb#L452

Capybara.configure do |config|
  # ...
  config.default_host = "http://www.example.com"
  # ...
end

You have to override this setting in your project config.


If it's not capybara it could be rails issue ? From the input of this thread https://github.com/rspec/rspec-rails/issues/1275 I would try something like this

# config/environment/test.rb
config.action_controller.default_url_options = {
  host: '0.0.0.0:3000' # or whatever your host is
}

In the thread I found:

# putting this into initializer of test framework
[ApplicationController, ActionController::Base].each do |klass|
  klass.class_eval do
    def default_url_options(options = {})
      { :host => "example.com" }.merge(options) # or localhost:8888 or 0.0.0.0:3000 or whatever your server is
    end
  end
end
Simon Franzen
  • 2,628
  • 25
  • 34
-1

It's a weird quirk but rails has two types of url_helpers for routes. something_url and something_path. The first is an absolute path, http://www.awesome.com/something) the second is the relative path (/someurl).

Unless you have a domain name set explicitly on your test environment, use the _path helpers in your tests. If you want to use the _url helpers then you need to configure a base url in your application's test environment (config/environments/test.rb)

engineerDave
  • 3,887
  • 26
  • 28
  • I know about the _path and _url. But your answer doesn't explain why I have example.com set by default. – Daniel Costa Mar 11 '18 at 13:26
  • well you may know about them but you're using _url in your tests with assert redirected_to root_url instead of root_path, and you don't have a domain set in your environment. https://stackoverflow.com/a/48865478/793330 – engineerDave Mar 12 '18 at 15:04
  • Could you elaborate on what is wrong on doing that? – Daniel Costa Mar 12 '18 at 23:02