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
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 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)
I have multiple Rails engines in my Rails 4 beta1 application. I'm installed rspec-rails gem to every engines. And I created my engine following command:
rails plugin new store_frontend --dummy-path=spec/dummy -d postgresql --skip-test-unit --mountable
In my engine's dummy application I configured database and routes. Here is example routes.rb file:
Rails.application.routes.draw do
mount StoreFrontend::Engine => "/store"
end
When I run rspec inside first engine I get following errors:
1) StoreAdmin::DashboardController GET 'index' returns http success
Failure/Error: get 'index'
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"store_admin/dashboard"}
# ./spec/controllers/store_admin/dashboard_controller_spec.rb:8:in `block (3 levels) in <module:StoreAdmin>'
And here is my controller test /It's generated from Rails/:
require 'spec_helper'
module StoreFrontend
describe HomeController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end
end
It seems like controller test is not working. I have model tests and it's working fine. Any idea?
UPDATE 1:
My application structure:
bin/
config/
db/
lib/
log/
public/
tmp/
engine1/
engine2/
engine3/
The solution is very simple. Add use_route to your controller test. Here is the example.
module StoreFrontend
describe HomeController do
describe "GET 'index'" do
it "returns http success" do
get 'index', use_route: 'store_frontend' # in my case
response.should be_success
end
end
end
end
The configuration and spec you show are for StoreFrontend but the error is for StoreAdmin::DashboardController. So it seems like you are just confused about which engine you are testing and/or which engine is failing.
Of course the simple solution is to create the missing route {:action=>"index", :controller=>"store_admin/dashboard"}
In order to get the routing correct when testing Rails engine controllers with Rspec, I typically add the following code to my spec_helper.rb:
RSpec.configure do |config|
config.before(:each, :type => :controller) { #routes = YourEngineName::Engine.routes }
config.before(:each, :type => :routing) { #routes = YourEngineName::Engine.routes }
end
I recently switched started using rspec-rails(2.6.1) with my Rails(3.0.8) app. I'm used to Test::Unit, and I can't seem to get a filter working for my test methods. I like to keep things as DRY as possible, so I'd like to set up a filter that I can call on any test method that will login as an Authlogic user before the test method is called. I tried accomplishing this by using an RSpec filter in spec_helper.rb:
config.before(:each, :login_as_admin => true) do
post "/user_sessions/create", :user_session => {:username => "admin", :password => "admin"}
end
Then I use it in the corresponding test method(in this case spec/controllers/admin_controller_spec.rb):
require 'spec_helper'
describe AdminController do
describe "GET index" do
it("gives a 200 response when visited as an admin", :login_as_admin => true) do
get :index
response.code.should eq("200")
end
end
end
However, I get this error when I run rspec spec:
Failures:
1) AdminController GET index gives a 200 response when visited as an admin
Failure/Error: Unable to find matching line from backtrace
RuntimeError:
#routes is nil: make sure you set it in your test's setup method.
Blech. Can I only send one HTTP request per test? I also tried stubbing out my authenticate_admin method(inside the config.before block), without any luck.
Unfortunately, there is no way at the moment to do what you're trying to do in a globally defined before hook. The reason is that before hooks are executed in the order in which they get registered, and those declared in RSpec.configure are registered before the one that rspec-rails registers internally to set up the controller, request, response, etc.
Also, this has been reported to https://github.com/rspec/rspec-rails/issues/391.
You should use shulda's macrons. To use shoulda modify your spec_helper.rb
RSpec.configure do |config|
config.include Clearance::Shoulda::Helpers
end
And then can setup filter in controller spec like
require 'spec_helper'
describe AdminController do
fixture :users
before(:each) do
sign_in_as users(:your_user)
end
describe "GET index" do
it("gives a 200 response when visited as an admin", :login_as_admin => true) do
get :index
response.code.should eq("200")
end
end
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