How can an rspec test for a view pass if there's no action for the view in the controller?

The rspec test looks like this:

require ‘spec_helper’

describe “Static pages” do

describe “Home page” do

it "should have the h1 'Sample App'" do
  visit '/static_pages/home'
  page.should have_selector('h1', :text => 'Sample App')
end

end

end

My controller looks like this:

class StaticPagesController < ApplicationController
end

And I’m running the test like this:

~/rails_projects/sample_app25$ rspec spec/requests/static_pages_spec.rb

How can the visit() method:

visit '/static_pages/home'

…work when there is no home action?

On 30 August 2012 22:29, 7stud – [email protected] wrote:

  visit '/static_pages/home'

class StaticPagesController < ApplicationController
end

If you go to that url in the browser does it show a page with the
header?

Is there a page in public/static_pages that matches that url? In
which case that will be served without the need for a controller
action.

What does test.log show when you run the test?

Colin

  1. Yes, the page shows when I start rails server, and then use the url:

http://localhost:300/static_pages/home

  1. Everything in public/ was created by rails new:

$ ls public/
404.html 500.html index.html
422.html favicon.ico robots.txt

  1. test.log:

Started GET “/static_pages/home” for 127.0.0.1 at 2012-08-31 10:04:28
-0700
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.erb within layouts/application (3.7ms)
Completed 200 OK in 21ms (Views: 20.0ms | ActiveRecord: 0.0ms)
[1m[35m (0.1ms)[0m rollback transaction
[1m[36m (0.1ms)[0m [1mbegin transaction[0m

Started GET “/static_pages/home” for 127.0.0.1 at 2012-08-31 10:04:28
-0700
Processing by StaticPagesController#home as HTML
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
[1m[35m (0.1ms)[0m rollback transaction
[1m[36m (0.1ms)[0m [1mbegin transaction[0m

Yet here is my controller:

class StaticPagesController < ApplicationController
end

Previously, I had actions in my controller, but I wanted to see if the
tests would fail without the actions, and I fully expected them to fail,
but they won’t. I have everything at github:

Curiously, if I write a test for a non-existent view, I get three
errors: First, no route; then after I add the route:

get ‘/static_pages/something’

I get an error saying there’s no action; then after I add the action, I
get an error saying there’s no view; then after adding a view, the test
passes. Subsequently, if I delete the action for that view, the test
still passes. Why?

More info:

$ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]
~/rails_projects/sample_app25$ rails --version
Rails 3.2.8
~/rails_projects/sample_app25$ rspec --version
2.10.1

Thanks for the response. First this:

$ rake routes
static_pages_home GET /static_pages/home(.:format)
static_pages#home
static_pages_help GET /static_pages/help(.:format)
static_pages#help
static_pages_about GET /static_pages/about(.:format)
static_pages#about
static_pages_contact GET /static_pages/contact(.:format)
static_pages#contact
~/rails_projects/sample_app25$

And here is my routes.rb:

SampleApp25::Application.routes.draw do
get “static_pages/home”
get “static_pages/help”
get “static_pages/about”
get “static_pages/contact”

The priority is based upon order of creation:

first created -> highest priority.

Sample of regular route:

match ‘products/:id’ => ‘catalog#view’

Keep in mind you can assign values other than :controller and

:action

Sample of named route:

match ‘products/:id/purchase’ => ‘catalog#purchase’, :as =>

:purchase

This route can be invoked with purchase_url(:id => product.id)

Sample resource route (maps HTTP verbs to controller actions

automatically):

resources :products

Sample resource route with options:

resources :products do

member do

get ‘short’

post ‘toggle’

end

collection do

get ‘sold’

end

end

Sample resource route with sub-resources:

resources :products do

resources :comments, :sales

resource :seller

end

Sample resource route with more complex sub-resources

resources :products do

resources :comments

resources :sales do

get ‘recent’, :on => :collection

end

end

Sample resource route within a namespace:

namespace :admin do

# Directs /admin/products/* to Admin::ProductsController

# (app/controllers/admin/products_controller.rb)

resources :products

end

You can have the root of your site routed with “root”

just remember to delete public/index.html.

root :to => ‘welcome#index’

See how all your routes lay out with “rake routes”

This is a legacy wild controller route that’s not recommended for

RESTful applications.

Note: This route will make all actions in every controller

accessible via GET requests.

match ‘:controller(/:action(/:id))(.:format)’

end

On Aug 31, 2012, at 2:49 PM, 7stud – wrote:

errors: First, no route; then after I add the route:

get ‘/static_pages/something’

I get an error saying there’s no action; then after I add the action, I
get an error saying there’s no view; then after adding a view, the test
passes. Subsequently, if I delete the action for that view, the test
still passes. Why?

What’s in your routes.rb? This could be a catch-all route firing. In
Ryan B.’ Railscast on semi-static pages, he mentions that a
completely empty controller will work as long as there is a properly
named view file at the expected path. I tried it in a quick test app,
but it didn’t work in vanilla 3.2.

Walter

On Aug 31, 2012, at 4:10 PM, 7stud – wrote:

Thanks for the response. Here is my routes.rb:

SampleApp25::Application.routes.draw do
get “static_pages/home”
get “static_pages/help”
get “static_pages/about”
get “static_pages/contact”

Yup. I can confirm that with a similar route here in an otherwise
entirely empty controller and a matching view in the correct folder,
this just works without any logic at all. Let’s hear it for convention
over configuration!

Walter

So this is a route “problem”?

It’s a feature of Rails. It’s a problem if it causes problems. With the
sort of routing you have, it’s only going to expose the views you
indicate in your routes.rb file. If you had a more liberal route like

match ‘info/:action’ => ‘info#action’

then it would show any action.html you placed in the views/info folder,
as long as it was requested properly.

Walter

On Saturday, September 1, 2012 11:05:36 PM UTC+1, Ruby-Forum.com User
wrote:

You can skip the adding an action bit and go straight to creating a view

But if I then delete the dog action, and then use the same url,
http://localhost:3000/static_pages/dog, in my browser, this time I get a
different result–instead of getting an error the view displays. For
me, that inconsistent behavior is not right, and it should not be that
way. So I want to know whether there were reasons for the rails team
to institute that inconsistent behavior, or whether it is a bug.

This is a long standing rails feature (right back to 1.x at least): rails
doesn’t make you define an empty action. I don’t think it’s a feature
that
gets a whole lot of use, but it’s definitely intentional (documented
here:
Layouts and Rendering in Rails — Ruby on Rails Guides
)

Fred

Thanks for the response Frederick C…

I don’t like this ‘default rendering’, nor the misleading error message
saying the action is missing–but I guess I’ll have to live with it.
According to the docs, a
route and view is all that’s necessary to render a page.

On 1 September 2012 23:05, 7stud – [email protected] wrote:

browser, rails complains that there’s no action called dog in the

But if I then delete the dog action, and then use the same url,
http://localhost:3000/static_pages/dog, in my browser, this time I get a
different result–instead of getting an error the view displays. For
me, that inconsistent behavior is not right, and it should not be that
way. So I want to know whether there were reasons for the rails team
to institute that inconsistent behavior, or whether it is a bug.

It is not inconsistent. You started with no action and no view and
url fails. You added action and view and url works. You deleted only
the action and the view continues to work. If you delete the view
then it will fail again. Perfectly consistent. In fact the empty
action is just not needed. Rails assumes that since you have provided
a route and a view that you would like them to work and saves you the
effort of providing an empty action. I never intentionally rely on
that myself as I find it a little confusing, but it is not
inconsistent.

Colin

It’s a feature of Rails. It’s a problem if it causes problems.

(Note: nothing in this post has anything to do with rspec.)

For me, that routing behavior is causing problems. Here’s why: if I add
a route such as:

get “static_pages/dog”

…and then enter the url http://localhost:3000/static_pages/dog in my
browser, rails complains that there’s no action called dog in the
StaticPagesController:

===
Unknown action

The action ‘dog’ could not be found for StaticPagesController

Then if I add the dog action to the controller, then create a view,
everything works fine and dandy.

But if I then delete the dog action, and then use the same url,
http://localhost:3000/static_pages/dog, in my browser, this time I get a
different result–instead of getting an error the view displays. As far
as I am concerned, that inconsistent behavior is not right.
So I want to know whether there were reasons for the
rails team to institute that inconsistent behavior, or whether it is a
bug.

As far as I know, the most basic concept of rails is that urls get
routed to controllers and actions. So what’s going on?

It is not inconsistent.

Okay, perhaps it’s more correct to say that Rails is flat out wrong.
Rails complains that I need an action when there isn’t one, which is
patently false. Is it asking too much to ask Rails not to lie to me (a
poor beginner)?

On 3 September 2012 04:29, 7stud – [email protected] wrote:

It is not inconsistent.

Okay, perhaps it’s more correct to say that Rails is flat out wrong.
Rails complains that I need an action when there isn’t one, which is
false.

What do you mean it complains that you need an action? I thought your
problem was that it did /not/ complain that there is no action in the
model.

Colin

You haven’t been paying attention.

On 3 September 2012 17:02, 7stud – [email protected] wrote:

If you don’t even know what the posts in this thread say, why bother
posting anything?

I couldn’t agree more.

Colin