RSpec includes help needed - ruby-on-rails

I'm trying to mimic something similar to http://relishapp.com/rspec/rspec-rails/v/2-2/dir/routing-specs/access-to-named-routes-in-routing-specs
I have a unit test passing:
require 'test_helper'
class RoutesTest < ActionController::TestCase
test "book name is sent to store#index' do
assert_routing 'book/mytitle', {:controller => 'book', :action => 'index', :title => 'mytitle'}
end
end
I'm trying to covert this to an RSpec test (running Rspec 2.2 under Rails3.0.3)
Here's the test:
require 'spec_helper'
include RSpec::Rails::Matchers::RoutingMatchers
include ActionDispatch::Assertions::RoutingAssertions
describe "book specific routes" do
it "should recognize title in path" do
{:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
end
end
But this results in:
Failures:
1) book specific routes should recognize title in path
Failure/Error: {:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
undefined method `recognize_path' for nil:NilClass
# ./spec/route_spec.rb:9:in `block (2 levels) in <top (required)>'
Any idea where the nilClass is coming from? Any help would be appreciated.

It's the double include of ActionDispatch::Assertions::RoutingAssertions which causes the failure -- not sure why. Remove the two include statements and all should be well. The spec file should live in /spec/routing. You can wrap the example with describe BooksController for style points, but it will work without it.

I'm guessing the matchers need to be used inside a controller spec. They should already be there, so there would be no need to include them manually. Just make sure you're describing a controller.
describe BooksController do
it "should recognize title in path" do
# ...
end
end

Related

Getting undefined method get on a named route in ruby on rails

I keep getting this error that I just can't figure out:
Failure/Error: get :find_movies, {:id => #id_passed }
NoMethodError:
undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x0000000363f800>
For the following rspec test:
it 'should find the movie in the database by the passed id' do
Movie.should_receive(:find).with(#id_passed).and_return(#movie_found)
get :find_movies, {:id => #id_passed }
end
which uses the following route:
get '/movies/find/:id' => 'movies#find', :as => :find_movies
And my controller includes:
def find
#movie = Movie.find params[:id]
if #movie.director != ""
#similar = Movie.where({:director => #movie.director}).all if #movie.director
else
flash[:warning] = "\'#{#movie.title}\' has no director info"
redirect_to movies_path
end
end
My friend pretty much wrote the exact same code and got it working. Can anyone help me figure out why this isn't working? Many thanks!
rspec may not be seem to take that your spec is a controller spec.You can add a type to your describe like this
describe YourController, :type => :controller do
...
end
Another fix may be by adding require 'rspec/rails' to spec_helper
If you are using 'spec/features', you may need to add the following to your 'spec_helper.rb'
config.include RSpec::Rails::RequestExampleGroup, type: :feature
See this answer for more : undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>

How can I test Rails Routes with Minitest?

I'm trying to use Minitest for an existing Rails app (3.2), but not having any luck running routing tests. I've tried rspec syntax (should route_to) and TestUnit syntax (assert_routing) but no luck.
Any advice on getting this to work? Specific modules I need to include, etc?
thanks
If you are using minitest-rails you can create route tests by placing the following in test/routes/homepage_test.rb:
require "minitest_helper"
class HomepageRouteTest < ActionDispatch::IntegrationTest
def test_homepage
assert_routing "/", :controller => "home", :action => "index"
end
end
Alternatively, you can use the Minitest Spec DSL:
require "minitest_helper"
describe "Homepage Route Acceptance Test" do
it "resolves the homepage" do
assert_routing "/", :controller => "home", :action => "index"
end
end
You can run these tests with the following rake task:
rake minitest:routes
#blowmage's answer helped me but it looks like the syntax has changed a bit.
With Rails 4:
require "test_helper"
class HomepageRouteTest < ActionDispatch::IntegrationTest
def test_messages
assert_generates '/messages', :controller => "messages", :action => "index"
end
end

How to test Devise in a Controller RSpec, error: undefined method `assign' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010597f4b8>

I'm getting the following error:
undefined method `assign' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010597f4b8>
When attempting to test per the docs.
Here's is what I have:
user_controller_spec.rb
require 'spec_helper'
describe "devise/sessions/new.html.erb" do
let(:user) do
stub_model(User).as_new_record
end
before do
assign(:user, user)
# Devise provides resource and resource_name helpers and
# mappings so stub them here.
#view.stub(:resource).and_return(user)
#view.stub(:resource_name).and_return('user')
#view.stub(:devise_mapping).and_return(Devise.mappings[:user])
end
it "renders a form to sign the user in" do
render
rendered.should have_selector("form",
:method => "post",
:action => user_session_path
) do |form|
form.should have_selector("input", :type => "submit")
end
end
end
Suggestions? Thanks
Rspec has changed a bit, it's now using assigns.
Doc here.

Rspec newbie: Quick example of nested controller test?

I'm just starting out with RSpec and having a little difficulty writing up controller tests for nested resources. I've tried googling this, but without much luck.
Could someone offer a basic example of a "PUT update" test test ensures a nested resource is updated? Just to elaborate, I have the equivalent (non-nested) resource tested like this:
def mock_post(stubs={})
#mock_post ||= mock_model(Post, stubs).as_null_object
end
...
describe "PUT update" do
describe "with valid parameters" do
it "updates the requested post" do
Post.stub(:find).with("14") { mock_post }
mock_post.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "14", :post => {'these' => 'params'}
end
end
end
I've been trying for some time to correctly stub a similar test for a 'Comment' model which is nested under Post, but no joy. Any suggestions appreciated.
You'll need to have both id's passed to your put method
put :update, :id => "14", :post_id=> "1", :comment => {'these' => 'params'}

How to use rspec to test named routes?

Given I have a named route:
map.some_route '/some_routes/:id', :controller => 'some', :action => 'other'
How do I use the routing spec file 'spec/routing/some_routing_spec.rb' to test for that named route?
I've tried this after the "describe SomeRouteController" block and it doesn't work, I get 'undefined method "helper":
describe SomeRouteHelper, 'some routes named routes' do
it 'should recognize some_route' do
helper.some_route_path(23).should == '/some_routes/23'
end
end
If this is in a controller spec, you can call the routing method directly, no helper needed.
describe SomeController do
it 'should recognize ma routes!' do
thing_path(23).should == '/things/23'
end
end
In RSpec-Rails 2.7+ you can create a spec/routing directory and put your routing specs in there. See the rspec-rails docs for more info.
there's a nice shoulda matcher for this too:
it { should route(:get, "/users/23").to(:action => "show", :id => 23)
more information on using shoulda matchers with rspec:
https://github.com/thoughtbot/shoulda-matchers
You can do this in your controller specs with the assert_routing method, like so:
describe UsersController do
it "should recognize a specific users#show route" do
assert_routing("/users/23", {:controller => "users", :action => "show", :id => 23})
end
end
More documentation is here.
This is how I write the specs using RSpec2, Shoulda, and Capybara. You would save this example file in #{Rails.root}/spec/routing/thingz_routing_spec.rb or my preference #{Rails.root}/spec/routing/thingz_controller_spec.rb
require "spec_helper"
describe ThingzController do
describe "routing" do
it "routes to #index" do
get("/thingz").should route_to("thingz#index")
end
end
end

Resources