I am new to Rails. I want to test my activeadmin resources as mentioned in the following link
https://github.com/activeadmin/activeadmin/wiki/Testing-your-ActiveAdmin-controllers-with-RSpec
My simple test as follows
require 'spec_helper'
describe "activeadmin resources" do
it "should have admin user resource" do
ActiveAdmin.application.namespaces[:admin].resources.should have_key("AdminUser")
end
end
But I am getting the error
NameError: uninitialized constant ActiveAdmin`
I tried to require activeadmin gem in spec_helper, but no use. Any idea?
Just by changing
require 'spec_helper'
to
require 'rails_helper'
the error was resolved.
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.
According to capybara's readme on Github I have to require capybara/rails in my spec_helper file after installing the gem:
require 'capybara/rails'
But even if I leave out the require statement in spec_helper, I can still use the following capybara syntax:
scenario "successfully creates a new user account" do
visit "users#index"
fill_in "user_email", with: "foo#bar.com"
click_button "Create User"
expect(page).to have_content "User successfully created"
end
Does it have something to do with the rails g rspec:install command? I am confused about why the syntax works; how does the app know?
All requiring capybara/rails does is set up Capybara.app and configure the rack-test driver to respect any data-method attributes on links. Other than that it doesn't affect capybara. If you've already configured Capybara.app some other way then things will work fine. You may also have it required in your rails_helper.rb rather than spec_helper.rb
I've upgraded to Rails 5. My first hurdle in getting specs to pass is a 'No route matches' error.
Please see my test and test_helper below. Is there anything I need to add to test_helper or test.rb? Anyone know the cause or how to resolve this?
.....
I've been running a single test while trying to simply get a pass:
bin/rails test test/controllers/users_controller_test.rb:31
which is the 'should get new' line in my users_controller_test.rb
require 'test_helper'
describe UsersController do
//class UsersControllerTest < ActionDispatch::IntegrationTest
before do
glenn = users(:glenn)
sign_in(glenn)
end
it 'should get new' do
get new_user_url
value(response).must_be :success?
end
end
this results in the following error.
Error:
UsersController#test_0002_should get new:
ActionController::UrlGenerationError: No route matches {:action=>"http://test.host/users/new", :controller=>"users"}
test/controllers/users_controller_test.rb:32:in `block (2 levels) in <top (required)>'
test_helper.rb
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/rails'
class ActionController::TestCase
include ActiveJob::TestHelper
include Devise::Test::ControllerHelpers
end
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
end
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
include ActionDispatch::TestProcess # fixture_file_upload
end
I had this same issue and was able to get it to work, though I'm not entirely sure about the internals of why it works.
Apparently the type of spec is necessary.
What I had that resulted in similar error as above:
require 'rails_helper'
RSpec.describe TimingsController, type: :controller do
describe "GET new" do
it "renders the new template" do
get new_timing_path
expect(response).to be_successful
end
end
end
What I have now that works:
require 'rails_helper'
RSpec.describe TimingsController, type: :controller do
describe "GET new", type: :request do
it "renders the new template" do
get new_timing_path
expect(response).to be_successful
end
end
end
So it seems like the type: request part is essential.
While troube-shooting this I created a new rails 5 app, installed devise and minitest-rails...and those tests are passing. The new url style syntax is working fine inside the describe block in my controller tests. However, in the app that pertains to this questions...the url syntax is not working inside the describe block and the fix---at least for now, was to replace the describe block with a class that inherits from ActionDispatch::IntegrationTest. I have no idea, yet, why this is.
Does setting host help for your case?
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-host
host! "subdomain.example.com"
So I am trying to run my test which is suppose to pass now but I keep getting an error
1) Sign in flow successful redirects to the topics index
Failure/Error: visit('/topics')
NoMethodError:
undefined method `visit' for #
<RSpec::ExampleGroups::SignInFlow::Successful:0x007fd91e36f8e0>
# ./spec/models/sign_in_spec.rb:10:in `block (3 levels) in <top (required)>'
Here is my sign_in_spec.rb file
require 'rails_helper'
describe "Sign in flow" do
include TestFactories
describe "successful" do
it "redirects to the topics index" do
user = authenticated_user
visit root_path
end
end
end
And my rails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
require 'capybara/rspec'
I am not sure but I don't think I need to create a route for root_path do I?
The issue isn't / -- it's visit as your error shows. That method doesn't exist in this spec file. That's a Capybara method, and Capybara is for feature specs.
It looks like the problem is that you're trying to run a "features" spec in a model spec file. Capybara, which creates the domain specific language you're using, sets it to be available only in certain spec folders. If you move the spec into the spec/features folder, that may fix it.
In some of my controllers I need to use the group(s) that belong to my logged in user(current_user.groups). When I try to test; I don't seem to have this current_user though:
ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
So I figured I should create that current_user with Devise.
I've read the documentation of Devise stating I should add the following to my test_helper.rb:
class ActionController::TestCase
include Devise::TestHelpers
def setup
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in FactoryGirl.create(:user)
end
end
This doesn't seem to do the trick though; Whenever I run rake test I get the following errors:
1) Error:
ActivitiesControllerTest#test_should_create_activity:
NameError: uninitialized constant ActionController::TestCase::FactoryGirl
test/test_helper.rb:22:in `setup'
You have to include the factory_girl_rails gem in your Gemfile. I usually include it in both the development and test group, but just the test environment is fine for your example.
group :development, :test do
gem 'factory_girl_rails'
end
And then run bundle install.
factory_girl_rails is used when you are creating the user fixture in your test:
sign_in FactoryGirl.create(:user)
Then you need to create a factory (which is almost like a fixture):
rails generate factory_girl:model user
This will create the file: test/factories/users.rb
Read more about factory_girl_rails and how to define factories here: https://github.com/thoughtbot/factory_girl_rails