I am new to Rspec and am trying to test the one route in my application. I have installed Rspec and have included the routing file in spec/routing/routes_spec.rb.
My spec is as follows:
require "spec_helper"
describe "Routes" do
it "routes get index" do
expect(:get => "simulations").to route_to(
:controller => "simulations",
:action => "index"
)
end
end
I get this error:
Routes routes get index
Failure/Error: expect(:get => "simulations").to route_to(
NoMethodError:
undefined method `route_to' for #<RSpec::ExampleGroups::Routes:0x007fc32d2f70b8 #__memoized=nil>
# ./spec/routing/routes_spec.rb:6:in `block (2 levels) in <top (required)>'
Any ideas as to why route_to would be undefined? I have verified that the route actually works.
In Rspec 3 you should require 'rails_helper' rather than require 'spec_helper'.
Based on documentation:
Routing specs are marked by :type => :routing or if you have set config.infer_spec_type_from_file_location! by placing them in spec/routing.
So, unless you set the previous option, you should begin your spec with:
describe "Routes", :type => :routing do
As Fire-Dragon-DoL suggests above, you might like to check that the rspec-rails gem is in place.
When you don't have rspec-rails installed and required, and you use to_route method, you will get the same error when running your specs: NoMethodError: undefined method 'route_to'
Given the same setup, when you use be_routable matcher, then you get another error, in the style of: expected {:get=>"/my_models/} to respond to 'routable?'
To remedy these errors
Add rspec-rails to Gemfile
Run bundle install
Add require 'rspec/rails' to spec_helper (or rails_helper)
Related
QUESTION: What have I done wrong that the route_to method remains undefined?
I'm very new to this but I'm trying to develop some route tests via the rspec gem.
My issue is that I am obtaining the error:
undefined method `route_to' for #<RSpec::ExampleGroups::RouteToHomepage
I have already looked through the API for this query, and I've already done the following:
Install gem 'rspec-rails'
In rails_helper.rb
require 'rspec/rails'
In my routing_spec.rb (where I am writing the routes)
require 'rails_helper'
describe "route to homepage" do
it "routes /home to index" do
expect(:get => "/homes").to route_to(
action: "index"
)
end
end
What exactly do I need to change or add, so the "route_to" method is defined? I've already read around and apparently it's defined in the "rspec-rails" gem, which I have, and already included.
From the documentation:
Routing specs are marked by :type => :routing or if you have set
config.infer_spec_type_from_file_location! by placing them in spec/routing.
You didn't say where the routing_spec.rb is located, but if it's inside the folder spec/routing/ then you could choose to enable the above config option.
Otherwise, or in general, you must do this:
require 'rails_helper'
describe "route to homepage", type: :routing do
it "routes /home to index" do
expect(:get => "/homes").to route_to(
action: "index"
)
end
end
Doing this will include the necessary RSpec helper that defines route_to, among other methods.
I am having a problem including a should have_selector problem with rspec:
This is my code:
describe "GET 'home'" do
it "returns http success" do
get 'home'
expect(response).to be_success
end
it "should have the right title" do
should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
end
I have included the following at the top:
RSpec.describe PagesController, :type => :controller do
render_views
My html5 has the following:
<title>Ruby on Rails Tutorial Sample App | Home</title>
and I get an error message saying:
Failures:
1) PagesController GET 'home' should have the right title
Failure/Error: should have_selector("title",
expected #<PagesController:0x007fceef586a90> to respond to `has_selector?`
# ./spec/controllers/pages_controller_spec.rb:14:in `block (3 levels) in <top (required)>
Can someone help with that?
rspec -v
3.0.2
rails 4.1.1
thank you in advance.
Rspec 3 does not include the capybara matchers in controller specs by default. You can change this for an individual spec by doing
include Capybara::RSpecMatchers
Or, in your spec helper
config.include Capybara::RSpecMatchers, :type => :controller
Your next issue is that recent versions of capybara don't allow you to test for the presence of invisible elements by default, and the title element is considered to be invisible. You should use the have_title matcher instead.
Hollo rubyist pal!
I had this problem and that was because of two things, first I did not use capybara gem, and second have_selector only accepts one of :count, :minimum, :maximum, :between, :text, :visible, :exact, :match, :wait keys and does not understand :content.
I am sure you solved the problem, but for those who have started to learn Ruby on Rails recently and encountered such a problem I should say to eleminate it first put capybara gem in your Gemfile as below:
group :development, :test do
...
gem 'capybara'
end
and run command
bundle install
then to test that your view has a specific title, in pages_controller_spec.rb file write
describe "GET #home" do
...
it "should have the right title" do
get :home
expect(response.body).to have_selector('title', :text => 'Ruby on Rails rocks')
end
end
I hope it would help someone.
Cheerio!
Checking content is view spec...
And controller spec means checking response status/path, render page/partials, instance variable filters params.
I am writing a spec to test the behavior of the mashup_controller when someone sends a query through a URL. I need to simulate the parameters contained in the URL, and i read that the post() method will do that, however when i get an error:
1) MashupController simulates query
Failure/Error: post :create
NoMethodError:
undefined method `post' for
#<RSpec::Core::ExampleGroup::Nested_1:0x980bc50>
# ./mashup_controller_rspec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.20199 seconds 1 example, 1 failure
Failed examples:
rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query
Here is my code:
require 'spec_helper'
require 'mashup_controller.rb'
describe MashupController do
it "simulates query" do
post :create
end
end
Sorry if I'm not making any sense. I am very new to rails and rspec. Any help would be appreciated. Thanks.
If the spec file is not under spec/controllers, methods like get and post will not be automatically made available by rspec-rails.
You either need to tag your spec:
describe MyController, type: :controller do
# ...
end
or include the module:
describe MyController do
include RSpec::Rails::ControllerExampleGroup
# ...
end
See the relevant code in rspec-rails.
Make sure you have gem spec-rails in your Gemfile
Your mashup_controller_rspec.rb should be under spec/controllers
I used gem rspec-rails instead of gem spec-rails.
In Rails 4, you can declare the type of the RSpec tests as :request and the spec file can be in any directory.
example: in spec/routes/users.rb
RSpec.describe 'UserRoutes', type: :request do
...
end
My solution is
describe MyController, type: :controller
...
end
I had a bunch of combined controller/view tests written with rspec. I added the Capybara gem and wrote some integrations tests which pass fine. The only problem is that now in all my controller tests, where I have
response.should have_selector("some selector")
rspec gives errors such as:
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa03e7ec>
when I run controller tests. I'm guessing that Capybara is being used in my controller tests and has overwritten some Rspec methods. How can I fix this?
# gemfile.rb
group :test do
gem 'rspec'
gem "capybara"
gem "launchy"
gem 'factory_girl_rails', '1.0'
end
# spec_helper.rb
RSpec.configure do |config|
config.include IntegrationSpecHelper, :type => :request
end
Here's an example of a failing test:
# spec/controllers/books_controller_spec.rb
require 'spec_helper'
describe BooksController do
render_views
it "should have the right page title" do
get :show, :id => #book.ean
response.should have_selector("title", :content => "Lexicase | " + #book.title)
end
end
and it's associated error:
1) BooksController GET 'show' should have the right page title
Failure/Error: response.should have_selector("title", :content => "Lexicase | " + #book.title)
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa8488c0>
# ./spec/controllers/books_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
You were probably using Webrat earlier, and has_selector? is a Webrat matcher. Capybaras doesn't have a has_selector matcher, it has a matcher called has_css. You may want to replace the "has_selector" with "has_css".
Capybara helpers only works within requests specs. Either create a new request spec, or pass in :type => :request in the describe block part, like so:
describe "test for the testing test", :type => :request do
it "should work with capybara" do
visit root_path
click_link "Home"
page.should WHATEVA
end
end
I realize this question was asked a long time ago, but I thought I would share anyway. GLHF
I'm writing a Rails plugin and I'd like to be able to test a controller within the plugin:
describe ReportsController, :type => :controller do
it "shows paginated reports if the user is authorized" do
get 'index'
response.should render_template("index")
end
end
unfortunately this results in the following error:
NoMethodError in 'ReportsController index action shows paginated reports if the user is authorized'
undefined method `get' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f7155e042a0>
Rails env in plugin's spec_helper.rb is loaded:
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
Any ideas?
Solved by adding:
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'spec/spec_helper.rb'))
to plugin_dir/spec/spec_helper.rb