0

I recently upgraded by current app to Rails 3.2.8 and can't get a few tests to pass. I made a helper method that merges certain hashes with the params hash and creates a link to the current page. I used technique I saw on: link_to the current page plus/merged with a param.

Here is a stripped down version:

def simple_link
  link_to "Page", params.merge(:c => nil)
end

Then I would test it with something like:

describe "simple_link" do
  it { simple_link.should == "<a href=\"/assets\">Page</a>" }
end

Which was passing in 3.2.6.

However, now I receive the rspec error No route matches {:c=>nil}. Either Rails, rspec, or capybara is thinking the hash is the route. If I add a proper route like: users_path, then url_for seems to pick up the hash. Testing on a browser, everything works fine, no errors. Am I doing something wrong, was something changed in 3.2.8 or is this a bug?

Community
  • 1
  • 1
d_rail
  • 4,109
  • 32
  • 37
  • Stripped down code doesn't help us. – sevenseacat Oct 29 '12 at 06:52
  • I get the same error from the code supplied as my real helper method. – d_rail Oct 29 '12 at 06:57
  • 1
    possibly related?: https://github.com/rails/journey/issues/42 although this happened when I upgraded *to* rails 3.2.6. The solution for this one (for me at least) was to add a line to my Gemfile: `gem 'journey', '1.0.3'` which sets journey back to v. 1.0.3. – Chris Salzberg Oct 29 '12 at 06:59
  • That looks like the same problem I'm having. However, 3.2.8 depends on journey 1.0.4. So, I will either have to use rails 3.2.6 or just live with it. Probably will live with it. Thanks – d_rail Oct 29 '12 at 07:11
  • But were you using journey 1.0.3 or 1.0.4 previously? If you were using 1.0.3 then that might explain why it came up now, but if you were using 1.0.4 then it must be something else. – Chris Salzberg Oct 29 '12 at 07:14
  • How about if you add `match ':controller/:action' if Rails.env.test?` to your routes, per this suggestion: https://github.com/rails/journey/issues/39#issuecomment-6465755 – Chris Salzberg Oct 29 '12 at 07:52
  • I was using journey 1.0.3 according to git diff. The route addition did not change anything. I will go through all the github comments tomorrow. At least, now I have an idea where to look and what is causing it. Thanks again. – d_rail Oct 29 '12 at 08:09
  • Using journey 1.0.3 fixes it. @shioyama, if you want to post that answer, I'll accept it. – d_rail Oct 29 '12 at 23:19
  • Posted it as an answer below. – Chris Salzberg Oct 29 '12 at 23:53

2 Answers2

1

Upgrading rails also upgrades the journey routing gem from 1.0.3 to 1.0.4. In that upgrade, there was a change to how routing treats constraints:

1.0.4 fixed a regression from Rails 3.1 where the router would return a route even if the constraints a user passed in didn't match anything.

Since you mention (in the comments) that downgrading journey to 1.0.3 fixes the problem, this change in journey is almost certainly the cause of the problem. Have a look at these github issues to find possible solutions:

Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82
  • Side note: As of now, downgrading to Rails 3.2.6 is required to use journey 1.0.3 – d_rail Oct 30 '12 at 00:35
  • Yes I'm stuck at 3.2.6, and now that I think about it this was the reason I stayed there. But when I tried upgrading the tests all passed... I believe this patch was what fixed it for me: http://stackoverflow.com/questions/12414400/basic-devise-spec-with-i18n#12414995 – Chris Salzberg Oct 30 '12 at 00:44
0

The error is the way I'm calling link_to from a helper and not providing a current request. For now, I can get around the error by stubbing url_for, but this does not allow me to test the params hash. So trying to find a better way.

it "should return the right tag" do
  self.stub!(:url_for).and_return '/bar'
  simple_link.should == "<a href=\"/bar\">Page</a>"
end
d_rail
  • 4,109
  • 32
  • 37