The following is my problem I have Rspec installed started a project in it every-time I want to test something have to require_relative it now this wasn't a problem until I wanted to finally test my controllers and cant seem to get the functions get , post constantly getting undefined function error.
If I put my controller like this
RSpec.describe ApplicationController, :type => :controller do
end
I am receiving uninitialized constant ApplicationController (NameError) if i require relative the application controller like this require_relative '../../app/controllers/application_controller' I get uninitialized constant ActionController (NameError) .
Can't seem to catch where the problem might be here is my Gemfile also
gem "rspec-rails", :group => [:test, :development]
gem "rspec", :group => [:test, :development]
group :test do
gem "capybara"
gem "factory_girl_rails"
end
I have been browsing the internet for 2 hours now viewd at least 10 other stackoverflow questions tried everything but nothing seems to be working ...
Everywhere I check they say that I should only need to add require 'spec_helper' and it should work but its not.
I can copy my rails_helper file also if needed but its the standard what came with it when I ran rails generate rspec:install .
Your help is much appreciated I really need to fix this because I am running out of time.
As of rspec-rails 3 the auto generated spec_helper.rb is split into two parts: spec_helper.rb and rails_helper.rb
The idea is that you can have specs that don't need rails loaded (and so are fast to load) as well as specs that do need rails loaded
For ones that do need rails loaded, like your controller spec you need to do
require 'rails_helper'
At the top, instead of requiring spec_helper.
Related
I'm followind the Rails Tutorial but have a problem in section 3.2.1, just before figure 3.6.
When running
$ bundle exec rspec spec/requests/static_pages_spec.rb
I get failure
Failures:
1) StaticPages GET /static_pages works! (now write some real specs)
Failure/Error: get static_pages_index_path
NameError:
undefined local variable or method `static_pages_index_path' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe7592b33b8>
# ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.00454 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # StaticPages GET /static_pages works! (now write some real specs)
here are the files :
spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
end
app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
config/routes.rb
SecondApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
end
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.3'
group :development do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.9.0'
gem 'guard-rspec', '0.5.5'
end
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'rspec-rails', '2.9.0'
gem 'capybara', '1.1.2'
gem 'growl', '1.0.3'
end
group :production do
gem 'pg', '0.12.2'
end
Any idea about what I did wrong ?
I was having this problem as well; the tutorial seems to have missed a step.
in static_pages_spec.rb, the line:
get static_pages_index_path
...should be changed to:
get static_pages_home_path
This is because there is no index method in static_pages_controller.rb. The index is instead called home.
I reviewed your code, however, and it seems your static_pages_spec.rb file does not match the tutorial, but I guess you're copying the code from another place? I do not see static_pages_index_path anywhere, except in your console error text, which seems odd.
This is my static_pages_spec.rb in its entirety (at this stage), which passes the test:
require 'spec_helper'
describe "StaticPages" do
describe "GET /static_pages" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get static_pages_home_path
response.status.should be(200)
end
end
end
After this, the tutorial (in Listing 3.12) superseeds this step, changing static_pages_spec.rb altogether, although forgetting to explicitly recommend the change. This makes my code above irrelevant, but hopefully it explains your error.
Have a closer look at your spec/requests/static_pages_spec.rb. Please make sure you've deleted get static_pages_index_path line.
You have to update your app/views/static_pages/help.html.erb page to contain 'Sample App' in the same way as you have done with home.html.erb.
I had the same problem and I figured it out in the following way. I am also a newbie (first Stack Overflow post... nervous), so I don't have the jargon down:
The reason I was getting this error is that I didn't create the staticpages controller described in Listing 3.4 of the tutorial (I think I deleted it messing around with the practice commands that follow Listing 3.4 teaching you how to ahem, delete things). The way to check if you don't have the staticpages controller is to go to:
sample_app/controllers/ and so I only had the application_controller.rb file there and no static_pages_controller.rb.
So you have to run the rails generate controller StaticPages home help --no-test-framework
command and get that controller in there.
You can double-check your work by going to localhost:3000/static_pages/home and seeing if there is actually something there.
Then edit, per the tutorial, the home.html.erb files and check back to static_pages/home to see if it actually says "Sample App".
If static_pages/home actually says "Sample App" and the test is still a failure when you run it, then only God can help you. Or maybe someone else on Stack Overflow. Good luck.
Have you deleted the public/index.html? This could be causing the problem.
When you add "config.include Capybara::DSL" to the bottom of the RSpec helper file, in the section right before this, you may have forgotten to save the page. That is done by pressing the Command + S keys together. Sometimes it is also a good idea to restart your Rails Server if something doesn't work the way you expect it to the first time.
The problem is that you are trying to run a generated spec that doesn't actually work yet. The tutorial tells you to replace the body of that spec with this:
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
Note that it says "visit", not "get", and so on...
This happened because the spec generator (rails generate integration_test static_pages) assumes that there is a valid RESTful resource with lots of named routes, including _index_path, but that's simply not the case for this controller in this tutorial.
I was facing a similar problem. When you edit the static_pages_spec.rb, it looks like you typed this command (in listings 3.9)
<editor name> static_pages_spec.rb
whereas you had to type
<editor name> spec/requests/static_pages_spec.rb
This will surely solve your problem.
When I visit my sign in page in a browser everything works fine.
When I visit my sign in page in an rspec integration/request test, I get the following error:
ActionView::Template::Error:
undefined method `title' for #<#<Class:0x00000007af9180>:0x00000007af32a8>
The title method is used by the view and defined in ApplicationHelper which devise seems to find when using the browser. However, during rspec integration tests, devise is unable to find the helper method.
Is there anything I should be stubbing? It seems wrong to be stubbing in integration tests. Any other ideas?
(This question is not about how to include devise helpers in integration tests. I'm manually filling in the sign in forms to authenticate).
Looks like this issue. (in some cases related to ActiveAdmin https://github.com/gregbell/active_admin/wiki/Use-spork)
Here I found a hack that works for me (REE 1.8.7, Rails 3.1, Capybara, Devise, active_admin).
However, this is not likely to be merged, so I forked spork-rails to here with that patch applied. And as you probably know I can point my Gemfile to that repo:
gem 'spork-rails', :git => "git://github.com/chopmo/spork-rails.git"
Not optimal but it gets the job done for now.
I had a similar problem using Cucumber when I installed devise:
undefined local variable or method `flash_block' for #<#<Class:0x007ffd0a28dae8>:0x007ffd0b2f6d58> (ActionView::Template::Error)
I solved it by including the module in env.rb
Spork.prefork do
include FlashBlockHelper
I hope this helps.
Inside /spec/support create devise.rb with this:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Make sure your spec_helper.rb includes:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
and that your specs have:
require 'spec_helper'
This has been causing some frustration recently...
It seems that using Factories in my cucumber tests, in some situations causes AssociationTypeMismatch errors such as:
MyModel(#65776650) expected, got MyModel(#28190030) (ActiveRecord::AssociationTypeMismatch)
These seem to happen when there is an association reference - as if the Factory created object is different to the real one. See this question for more details: Cucumber duplicate class problem: AssociationTypeMismatch
I have been gradually changing Factory calls to real Model.create or mock_model calls. It would be nice to keep using Factory girl... I wonder if there is something I may have done wrong?
Thank you
I had this happening with me on Rails 3.1.0 rc5, and got it working.
To expand on Jonas' answer.
You should change your Gemfile to be like this:
gem 'factory_girl', '~> 2.0.0', :require => false
gem 'factory_girl_rails', '~> 1.1.0', :require => false
And then if you are using Spork, make your spec/spec_helper.rb file look like this:
Spork.each_run do
require 'factory_girl'
require 'factory_girl_rails'
end
It seems to happen if ActiveSupport unloads and reloads a constant that you have a reference to.
I've experienced the same with Rspec/Capybara, and what helped was a mixture of different things:
Make sure you have cached_classes set to false in your test environment (config/environments/test.rb)
In your gemspec, try replacing require 'factory_girl_rails' with 'factory_girl'
I'm using Spork (a test server), which seems to make this stuff increasingly difficult.
If you are using a test server, evaluate whether you should put ', :require => false' after factory_girl in your gemspec.
The topic is also covered in this google groups thread
Please let us know if any of this helped.
If you're using Spork, make sure to reload your factories after reloading your models.
E.g.
Spork.each_run
if Spork.using_spork?
print "Reloading models ... "
ActiveSupport::Dependencies.clear
puts "done"
print "Reloading factories ... "
FactoryGirl.reload
puts "done"
end
end
This happens because cache_classes is false, as is required by Spork. Capybara reloads Rails classes for every request (or, to be correct, Rails' reloader middleware does, which is not called for normal tests), and this freaks out the factories (exactly why, I'm not sure). You can either reload them, or simply run your Capybara specs outside of Spork.
So you need two things: to only run Capybara outside of Spork, and to set cache_classes to false only for Spork.
To only run Capybara outside of Spork, I have a Guardfile that runs specs in spec/requests outside of Spork and other specs inside of Spork here:
https://gist.github.com/1731900
Then, in config/environments/test.rb:
config.cache_classes = !ENV['DRB']
Your Capybara specs will be a bit slower, as they need to boot rails, but everything will Just Work.
I had some success with reloading the Factory definitions try something like this:
class Factory
def self.reload_definitions #:nodoc:
self.factories.clear
definition_file_paths.each do |path|
load("#{path}.rb") if File.exists?("#{path}.rb")
if File.directory? path
Dir[File.join(path, '*.rb')].each do |file|
load file
end
end
end
end
end
I ran into this issue when I passed the "class" option to my factory that was inherited by other factories:
factory :draft_resource, :class => Resource do
factory :resource, :parent => :draft_resource do
The only solution I could find was to simply not do this.
I ran into this same issue and spent probably ten hours trying every solution on this thread and everywhere else on the web. I started ripping out huge chunks of code trying to get it as close to another app of mine in which I couldn't reproduce the problem. Finally, I came across some helper functions in my spec_helper file:
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token if defined?(cookies)
end
A sign_in helper intended to work both in controller and request specs. And it does, sort of--just not with spork. When I removed the capybara helpers the issue was resolved:
def sign_in(user)
cookies[:remember_token] = user.remember_token
end
I have posted this in other places but no response. Trying to get Shoulda working inside Test/Unit in Rails 3.0.3 (1.9.2). When I try to run the test (copied below), I get this error:
test/unit/practice_member_test.rb:4:in <class:PracticeMemberTest>': undefined methodcontext' for PracticeMemberTest:Class (NoMethodError)
Note that I have another Rails 3 project with Rspec including Shoulda also and it works fine via Rspec. In the failing project I tried placing "require 'shoulda'" in test helper to no avail, but when I run the debugger and type Shoulda, the object is found, so the library is being loaded.
Here is my test:
require 'test_helper'
class PracticeMemberTest < Test::Unit::TestCase
context "practice member" do
should "get global practice member count not including Demo Practice" do
assert_equal PracticeMember.practice_members_global_count, 0
practice = Factory.create(:practice_one)
practice_member = Factory.create(:practice_member)
practice_member.practice_id = practice.id
practice_member.save
practice_member = Factory.create(:practice_member)
practice_member.practice_id = practice.id
practice_member.save
assert_equal PracticeMember.practice_members_global_count, 2
end
end
end
Must be something I am overlooking as I have not seen anyone with this same issue.
Did you try adding the following to your config/environment.rb file:
Rails::Initializer.run do |config|
config.gem "shoulda", :lib => "shoulda"
end
Then
$ rake gems:install
$ rake gems:unpack
As suggested in the documentation?
I have a bunch of config.gem statements in my environment.rb file:
config.gem "fastercsv", :version => "~>1.5.0"
config.gem "parseexcel", :version => "~>0.5.2"
config.gem "geokit", :version => "~>1.4.1"
config.gem "facebooker", :version => "~>1.0.50"
...
If I do "rake gems:install" then I get this issue:
rake aborted!
no such file to load -- fastercsv
Well...i know there is no such file to load because I am trying to install it. I suspect this may result from the location of my require. I have a module in my lib directory:
module SmartContactsImporter
require 'fastercsv'
require 'parseexcel'
...
Maybe Rails doesn't like me requiring a gem there but it seems silly since there is nothing wrong with having your module depend on a gem. Any ideas on how to solve this issue?
UPDATE
Turns out that this issue also occured with mechanize, geokit, and the list is continuing. It's a bit strange that config.gem doesn't work pretty easily out of the box. FYI I'm not freezing my gems.
If you leave out the require in SmartContactsImporter this should work (config.gem "fastercsv" will do the require for you).
You can work around it when a require is needed in environment.rb with a:
begin
require "rack/cache"
rescue LoadError
STDERR.puts "not loaded rack/cache: #{$!}"
end
This is ugly but it does do the trick.
You shouldn't require inside your module, config.gem will require it for you.
There's also a related issue with config.gem where it will attempt to require a dependent gem that is not yet installed whilst installing the gems, but this doesn't appear to be the case yet.