0

I have a Model (Show) in Rails that is accessed via a subdomain rather than a standard REST URL. In the file app/helpers/url_helper.rb I have the following method:

def show_url(show)
  root_url(subdomain: show.subdomain)
end

In controllers, this works perfectly. I can test it with puts show_url(@show) and it outputs the subdomain of the show as expected: http://test.example.com. In integration tests, however, the method doesn't work, and the default one generated by rails is used instead. If I run puts show_url(@show) there, I just get http://example.com. How do I use this custom URL helper in my integration tests?

Edit:

routes.rb section regarding this subdomain stuff:

constraints(lambda do |request|
    request.subdomain.present? && request.subdomain != 'www'
  end) do
  get '/' => 'shows#show', as: :show
  get '/edit' => 'shows#edit', as: :edit_show
end

This is based loosely around a Railscast on subdomain matching.

RedBassett
  • 3,469
  • 3
  • 32
  • 56
  • This might help... http://stackoverflow.com/questions/4160417/how-to-import-rails-helpers-in-to-the-functional-tests – Lanny Bose Jun 11 '15 at 20:48
  • I'm not getting much from that question. My issue is not with testing the URL helper (I have tests for it that work fine), but specifically that the helper method isn't available inside integration tests (however the default version generated by Rails is). – RedBassett Jun 12 '15 at 16:55
  • Ah, ok...maybe this question works for you? http://stackoverflow.com/questions/6721952/rails-helpers-not-working-in-test-environment – jemminger Jul 20 '15 at 19:18

2 Answers2

1

Try defining its route without the default "show" action:

# config/routes.rb
resources :show, except: :show

Sounds a bit confusing since your model is called Show, but what it's doing is defining all the standard restful routes (index, new, create, edit, update, delete) except for "show", e.g.

Or another way:

resources :show, only: %w(index new create edit update delete)
jemminger
  • 5,133
  • 4
  • 26
  • 47
  • Yeah, the names a tad confusing, but I get it. Let me give that a try! – RedBassett Jul 20 '15 at 16:07
  • So, it turns out, I don't use the `resources` method at all. I've manually defined the `show` and `edit` routes. See initial question for new code. – RedBassett Jul 20 '15 at 16:17
  • 1
    Ah, ok...maybe this question works for you? http://stackoverflow.com/questions/6721952/rails-helpers-not-working-in-test-environment – jemminger Jul 20 '15 at 16:46
  • Tried out the accepted answer there. Do I have to include that file, or are initializers loaded automatically? I'm just getting a `NameError`: `NameError: Couldn't find UrlHelper, expected it to be defined in helpers/url_helper.rb` – RedBassett Jul 21 '15 at 00:29
  • You would want to do it in an initializer like in the accepted answer: http://stackoverflow.com/a/6822329/126636 – jemminger Jul 21 '15 at 02:01
  • I added the initilizer, but I get the error above. Do I need to require the initializer in any way, or is it automatically loaded by Rails? – RedBassett Jul 22 '15 at 16:40
  • You do not need to require initializers, Rails will autoload any ruby files in `config/initializers` for you. Are you using the code from the answer, beginning with `ActionView::Helpers::UrlHelper.class_eval do`, not the code from the question starting with `module UrlHelper`? It works for me without error, though I had to move the line `alias_method_chain :url_for, :subdomain` to *after* the definition of `url_for_with_subdomain`, not before it as in the linked answer. – jemminger Jul 22 '15 at 19:37
  • Yes, i am using the `ActionView::Helpers:…` one, and I too had to move the `alias_method_chain` down. That's what's providing the error above. – RedBassett Jul 22 '15 at 22:37
0

I would really consider doing some refactoring and renaming the Show model.

piratebroadcast
  • 291
  • 5
  • 15
  • How come? Because of the common name with an action? Can you provide more information? This may be better suited as a comment than an answer. – RedBassett Jul 20 '15 at 16:14