I have successfully integrated rspec gem.. some of my tets related to redirect pass too. But when i use have_tag or have_text or have_selector , none of it works... I have added render_views on top of my controller spec file, still I get the error:
Capybara expection not met!!!!
What am I supposed to do to assert/verify an element on a page that is redirected to navigated to after some action in my test???
Error:
Failure/Error: response.body.should have_selector(:xpath, "//*[#id='header'
]/h2")
Capybara::ExpectationNotMet:
expected to find xpath "//*[#id='header']/h2" but there were no matches
# ./spec/controllers/account_controller_spec.rb:11:in `block (3 levels) in
<top (required)>'
Test:
describe AccountController do
render_views
describe "GET 'index'" do
it "should redirect to /users/sign_in" do
get 'welcome'
#response.should redirect_to("/users/sign_in")
response.body.should have_selector(:xpath, "//*[#id='header']/h2")
end
---- some more tests---
Update:
added this to spec_helper.rb, still no luck!
config.include Capybara::DSL
The purpose of controller tests is to validate the behavior of the controller. As described in https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs:
It allows you to simulate a single http request in each example, and
then specify expected outcomes such as:
rendered templates
redirects
instance variables assigned in the controller to be shared with the view
cookies sent back with the response
Testing content on a page that the controller has redirected you to is simply not in the scope of such tests. Just as it doesn't render views by default, it doesn't navigate to redirected pages.
To verify content on a page that you've been redirected to you, you can use a feature spec or, if you don't want to use Capybara, a request spec.
All the matchers like have_selector, have_tag, have_text started working after I added the following to spec_helper.rb
config.include Capybara::RSpecMatchers
Related
In my rails app, I have two layouts/controllers for different actions.
Eseentially, I match the root / to gateway#index, along with a few other pages such as /login and /register
The actual app once logged has its own sets of URLs, such as /dashboard /dashboard/action /explore etc.
Because of the pushstate with IE, the url changes to /#dashboard and loads the layout/JS for the gateway pages.
My rails controller for root has the following code, which is resulting in an endless loop in all versions of IE
if #current_user
redirect_to '/dashboard/lists'
end
The following is the Backbone history initializer (coffeescript):
Backbone.history.start
pushState: true
root: '/dashboard/'
Even with this setting, the application renders the gateway layout/JS not the application, and keeps the faulty URL the same (not setting the root to /dashboard).
How can I have IE load the application layout/JS/CSS while still having a different layout for the root?
I was setting the root to an invalid route. I ended up doing the following:
Backbone.history.start
pushState: true
root: '/app/'
And creating a route to a controller that used the application template.
From the Backbone website:
For RESTful persistence, history support via Backbone.Router and DOM manipulation with Backbone.View, include json2.js, and either jQuery.
So have you included json2.js?
I did
rails generate controller home index
But it adds this line to my routes.rb
get "home/index"
I thought Rails could deduce controller/method from the URL automatically? Why do I need to specify every get/post page?
Here's my complete routes.rb file:
Callisto2::Application.routes.draw do
root :to => "home#index"
resources :assets
end
root "/" works fine. so does /assets/*.
What's the problem with /home/index? I get the error:
Routing Error
No route matches [GET] "/home/index"
Try running rake routes for more information on available routes.
rake routes (run as apache user) gives me the following output:
root / home#index
Thanks for any clarifications. Not sure what I'm missing.
Edit: I didn't make this clear: I manually removed get /home/index from routes.rb to keep that file clean.
Rails used to add the so called catch all route at the bottom of your routes file:
match ':controller(/:action(/:id(.:format)))'
There was nothing 'automatic' or magical about these urls, just that every rails app started out with this route in their routes.rb
This has fallen out of favour, at least partially because it makes everything accessible over get, whereas
resources :books
Adds each route with the appropriate http verb. Listing routes explicitly is also a lot less verbose than when rails started out.
If your controller is home and the action is index your path is just /home.
You can find more information here.
I followed the basic steps to add authentication to Rails using Devise from their page, but every time I try to visit a default page (such as the Sign In or Sign Up pages), I get:
Routing Error
No route matches {:controller=>"devise/Home"}
This happens whether I link to the page in a view using
link_to('Register', new_user_registration_path)
or just visit "/users/sign_up".
This is a different error from when I visit a page with no route defined (No route matches [GET] "/users/bad_example"), and
devise_for :users
is already present in my routes.rb. I have even tried generating views (rails g devise:views) to no avail. It looks like Devise isn't generating/using a controller or some such. How do I go about fixing this?
Here are some files that may help:
routes.rb
rake routes output
It turns out the problem was not with Devise, but with my routes file. I have fixed my routes file as seen at http://guides.rubyonrails.org/routing.html and now this works properly.
I'm integrating capybara to a project. At first instance, I just wanted to check what is displaying the login page, so I made this code:
require 'acceptance/acceptance_helper'
feature 'Login' do
scenario 'sign in with right credentials' do
visit '/'
save_and_open_page
end
end
But when I run the test, it shows me:
Failure/Error: visit '/'
ActionController::RoutingError:
No route matches "/login"
# ./spec/acceptance/login_spec.rb:6
If I enter to the application without a valid session, it redirects me (code 302) to a rubycas server to log me in (which has the /login context at start) and after that it redirects me again to my server.
What should I do to just view the login page or how to maintain the redirection references in capybara?
I usually do this:
Inside my test.rb :
require 'casclient'
require 'casclient/frameworks/rails/filter'
CASClient::Frameworks::Rails::Filter.configure(
:cas_base_url => cas_base_url
)
Modify your spec like this:
CASClient::Frameworks::Rails::Filter.fake(username)
visit '/'
Let me know if this works for you.
==================================================================================
Without using "CASClient::Frameworks::Rails::Filter.fake(username)" I too get the same error.
The reason is that cas redirects you to its base url with "/login?service=http%3A%2d%2Fwww.example.com%aF" a parameter like this.
And hence it complaints about missing route.
Check your test log
Make sure you have added the proper matching routes. it seems that you are missing routes.
I hope this helps you.
For writing routes you can get help from http://railscasts.com/episodes/79-generate-named-routes
I am planning to create an app in rails but first I want to make a launch page. Having never made a launch page I am curious as to how others are doing it?
Do you creae a small rails application with controller and model that just collects email addresses? and then deploy the rails app? I'd prefer this way but it seems like an overkill to deploy a rails app just for a launch page...?
Also, how do you modify the routes file so that if users type anything after the url then only page that shows up is the laungh page.
Meaning, if my launch page is at http://mycoollaunchpage.com then if users mess around and type http://mycoollaunchpage.com/lkjlkjljk then it should redirect back to http://mycoollaunchpage.com
Your idea sounds good. Just a page with an email signup form would work well.
To redirect back to your home page, make a route glob in your routes.rb file, and have an action in your controller that just redirects back to your root.
# in routes.rb
match "*whatever", :controller => 'pages', :action => 'redirect_to_root'
# in your pages_controller.rb file
def redirect_to_root
redirect_to "/"
end
There is an awesome rails plugin available for this very requirement of yours ;)
https://github.com/vinsol/Launching-Soon/