Failing cucumber test after upgrading rails to 3.2.11 - ruby-on-rails-3

This one has had me stumped for a couple of days..
I have a cucumber test which fails with rails 3.2.11 but passes on rails 3.2.3. I'm getting a no route matches error however when I go through the exact same steps manually on the website it works.
My routes are -
get 'reward_player/:id' => 'reward_players#show', :as => :reward_player
And the error I am getting is
No route matches {:controller=>"admin/reward_player", :action=>"show", :id=>nil}
My step to visit this page is just click_link "Reward Player". When I use save_and_open_page to view the page the id is in the link. I'm really not sure what is going on or if it is a test that should have always been failing.

So it ended up being my route, it should have been
get 'reward_player/:user_id' => 'reward_players#show', :as => :reward_player
seeing as the controller was looking for a user_id in the params. So ended up being a test that shouldn't have really pass to start with.

Related

trouble getting started with requests testing

Go gentle on me: I've got the flu and only a few of my neural synapses are firing!
Here's a simple requests test for RSpec:
require 'spec_helper'
describe 'Home Page' do
it 'should mention Home' do
get '/'
response.body.should have_content("Home")
end
end
Great. It works. Now I want to verify that there's an image loaded when visting the home page as well. I assume there's a matcher for images similar to have_content(), so I first go looking for the definition of have_content().
Not found in
https://www.relishapp.com/rspec/search?query=have_content
http://guides.rubyonrails.org/testing.html
http://api.rubyonrails.org/
But the I remember that RSpec has nifty naming rules for matchers, so (e.g.) even?() => be_even(). But even then, searching for "content" in the above doesn't find anything.
(As an aside, I'm pretty sure I'm not looking for the Capybara method of the same name, since I'm doing a get and not visit. Right?)
At the risk of getting this question rejected for being too vague: where the heck is this method coming from, and where do I learn what else I can pass to response.body.should?
RSpec request specs use Capybara, so you can use either get or visit.
If you want to just check that the page has an <img> element with the correct link, you could use:
response.should have_selector('img', :src => '...')

How do I rspec with valid_session using Facebooker2 in rails 3?

I'm pretty new to Rails. I have been developing my app with RSpec. I have just followed a tutorial for Facebooker2 and added the before_filter for authentication.
#app/controller/application_controller.rb
before_filter :ensure_authenticated
However, my specs such as the one below failed because there's no valid session which is understandable.
#spec/controllers/answers_controller_spec.rb
describe "GET index" do
it "assigns all answers as #answers" do
get :index, {}, valid_session
assigns(:answers).should include(answers(:reading))
end
end
But I can't seem to find a way to fake a valid session for rspec to use in the following function.
#spec/controllers/answers_controller_spec.rb
def valid_session
{}
end
Is there a way at all? If not, what is the rails way of doing things for such a case?
If you aren't concerned about testing the actual authentication and just want to simulate a valid session, you could stub out the filter:
controller.stub(:ensure_authenticated).and_return(true)
EDIT: you may need ApplicationController.stub(...)

Hide id from url in rails

I am new to rails. I have developed an application on rails recently. The application is pretty big and it's running fine. Currently i have url like this.
http://192.168.99.220/user/13/domainUsers
I want it to be like the below one (without any id)
http://192.168.99.220/user/domainUsers
My routes are like this.
match 'user/:id/domainUsers', :to => 'domains#manageDomain_2', :as => :manageDomain2
I have tried to rewrite the url using "to_param". As my application is too big and it has lots of functionalities, i am using parameters other than the "id" to find users informations frequently, so i am not being able to use the "to_param" method. Is there any other way to hide "id" from url.
Please help
Thanks in advance.
The easiest way to do this is with a gem called friendly_id.
Here is a tutorial that explains it quite well:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid

RSpec2 and Capybara

Capybara is confusing me. If I use Capybara in combination with Ruby on Rails 3 and RSpec 2, then in RSpec request tests, the following matcher works:
response.body.should have_selector "div.some_class"
The response object has the class ActionDispatch::TestResponse. But the following line, which should work officially, does not work:
page.should have_selector "div.some_class"
The page object has the class Capybara::Session. In which cases do you have to use the response.body object and when do you have to use a page object ?
So I just ran into similar, and this is I think what's going on:
It depends on code you didn't include here of how you visit the page. I'm writing an rspec request spec.
If I retrieve the page with rspec's own:
get '/some/path'
then response.body.should have_selector works as you say, but page.should does not.
To make Capybara 'page' work (and to make Capybara interactions like click_button or fill_in work), instead of retrieving with rspec's 'get', you need to retrieve with Capybara's 'visit':
visit '/some/path'
page.should have_selector("works")
'page', a capybara method, only gets set when using 'visit', a capybara method.
This does get confusing, all the mixing and matching of different libraries involved in rails testing.
You would use response when you want to use the standard rails methods. And, alternately, you'd use page when you want to use the capybara methods. In capybara you'd most likely use have_css in the example given.

Why is this route in Rails 3 with a custom action not working? (better way?)

I have the following code in my routes.rb:
match 'users/linkedin' => "users#linkedin", :as => :register_linkedin
My expectation is that when I have a redirect_to register_linkedin_url, I will be redirected to domain.com/users/linkedin.
That should then result in Controller Users with action linkedin being executed.
This is what I get in the logs:
Redirected to
http://localhost:3000/users/linkedin
Completed 302 Found in 28333ms
Started GET "/users/linkedin" for
127.0.0.1 at Thu Apr 14 01:12:01 -0700 2011 Processing by
UsersController#show as HTML
Parameters: {"id"=>"linkedin"}
Completed in 94ms
This is what I get in 'rake routes':
register_linkedin
/users/linkedin(.:format)
{:action=>"linkedin",
:controller=>"users"}
So the routes aren't working properly. How do I address this?
it's because you're probably using resources :users in your routes
if you want to keep your show action, and use the linkedin part too, put your custom route before the resources :users
doing that, routing will match linkedin if the request is specific (users/linkedin) and if not, will continue searching and the next one will be show of course
read more about routing in general here
Routes are matched in the order they are defined. Put more specific routes higher than less-specific ones.
In your case, just make sure you define your custom route for users/linkedin before your more generic resources :users route is defined.