Uninitialized constant Production::POverview (NameError)
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/simon_zhu/Documents/original_version_carmel/carmel/spec/factories/poverview.rb:1:in `<top (required)>'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/find_definitions.rb:19:in `each'
I have the following code:
poverview.rb (factory)
FactoryGirl.define do
factory :poverview, class: Production::POverview do
name "test"
status ["p", "d", "m"]
end
end
p_overview_controller_spec.rb (spec)
require 'spec_helper'
describe Production::POverviewController do
login_user
# GET Request to pod_info
describe 'GET pod_info' do
before(:each) do
#pods = Factory(:poverview)
get 'show', :format => :json, :name => #pods.name
get 'show', :format => :json, :status => #pods.status
end
it "should return the correct company when correct id is passed" do
body = JSON.parse(response.body)
for(pod in body[0])
if(pod['name'].eql? #pod.name)
#pod.status.should.include? pod['status']
end
end
end
end
This is my first time writing an integration test for Factory Girl and I have this uninitialized constant error.
Any Ideas how to solve this?
Also your test setup looks very odd. It definitely looks like you are confusing integration and controller test.
Here is a post to get you started. I recommend doing some more research on testing in rails.
https://semaphoreapp.com/blog/2014/02/11/rails-testing-antipatterns-controllers.html
I have just started with RSpec and Capybara but got stuck on my first test.
Here's me test code located in spec/features/pages_spec.rb :
require 'rails_helper'
RSpec.describe "Pages", :type => :request do
describe "About Page" do
it "should have the content 'About Us'" do
visit '/pages/about'
page.should have_content('About Us')
end
end
end
Running the test i get the following error :
01:06:59 - INFO - Running: spec
F
Failures:
1) Pages About Page should have the content 'About Us'
Failure/Error: visit '/pages/about'
NoMethodError:
undefined method 'visit' for #<RSpec::ExampleGroups::Pages::AboutPage:0x007f975afe7380>
# ./spec/features/pages_spec.rb:6:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:44:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:43:in `block (2 levels) in <top (required)>'
Finished in 0.02569 seconds (files took 1.65 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/pages_spec.rb:5 # Pages About Page should have the content 'About Us'
I've been searching about this for about an hour and everywhere I find the solution of moving the test code from spec/requests to spec/features.
I've also seen this here : http://www.rubydoc.info/gems/rspec-rails/file/Capybara.md which most of the solution suggests and it is not recommended to use.
# not recommended!
RSpec.configure do |c|
c.include Capybara::DSL, :file_path => "spec/requests"
end
I've no idea how to proceed. Your help will be appreciated.
Either take :type => :request out of the describe block (which overrides the spec type determined from the directory location) or change it to :type => :feature.
I am trying to dry up my testing and would like to check routes. app.send("home_path") returns "/home" in the console but when I run it in tests it comes back as an undefined method.
Any ideas?
1) StaticPages Check paths should have content Home
Failure/Error: visit app.send("#{term.downcase}_path")
NoMethodError:
undefined method `home_path' for #<test::Application:0x007f917a48e078>
# ./spec/requests/static_pages_spec.rb:18:in `block (4 levels) in <top (required)>'
["Home", "About"].each do |term|
describe "Check paths" do
before(:each) do
visit app.send("#{term.downcase}_path")
end
it "should have content #{term}" do
expect(page).to have_content(term)
end
end
end
I am getting an undefined method for 'should' in my controller tests and cannot figure out why. I spent some time on google and stack overflow, but am stuck. Any help?
Spec Helper:
require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
end
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
#config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
Capybara.javascript_driver = :webkit
Controller Test:
require 'spec_helper'
include Devise::TestHelpers
describe PagesController do
# This should return the minimal set of attributes required to create a valid
# Page. As you add validations to Page, be sure to
# update the return value of this method accordingly.
def valid_attributes
{ }
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# PagesController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all pages as #pages" do
page = Page.create! valid_attributes
get :index, {}, valid_session
assigns(:pages).should eq([page])
end
end
describe "GET show" do
it "assigns the requested page as #page" do
page = Page.create! valid_attributes
get :show, {:id => page.to_param}, valid_session
assigns(:page).should eq(page)
end
end
describe "GET new" do
it "assigns a new page as #page" do
get :new, {}, valid_session
assigns(:page).should be_a_new(Page)
end
end
describe "GET edit" do
it "assigns the requested page as #page" do
page = Page.create! valid_attributes
get :edit, {:id => page.to_param}, valid_session
assigns(:page).should eq(page)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Page" do
expect {
post :create, {:page => valid_attributes}, valid_session
}.to change(Page, :count).by(1)
end
it "assigns a newly created page as #page" do
post :create, {:page => valid_attributes}, valid_session
assigns(:page).should be_a(Page)
assigns(:page).should be_persisted
end
it "redirects to the created page" do
post :create, {:page => valid_attributes}, valid_session
response.should redirect_to(Page.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved page as #page" do
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
post :create, {:page => { }}, valid_session
assigns(:page).should be_a_new(Page)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
post :create, {:page => { }}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested page" do
page = Page.create! valid_attributes
# Assuming there are no other pages in the database, this
# specifies that the Page created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Page.any_instance.should_receive(:update_attributes).with({ "these" => "params" })
put :update, {:id => page.to_param, :page => { "these" => "params" }}, valid_session
end
it "assigns the requested page as #page" do
page = Page.create! valid_attributes
put :update, {:id => page.to_param, :page => valid_attributes}, valid_session
assigns(:page).should eq(page)
end
it "redirects to the page" do
page = Page.create! valid_attributes
put :update, {:id => page.to_param, :page => valid_attributes}, valid_session
response.should redirect_to(page)
end
end
describe "with invalid params" do
it "assigns the page as #page" do
page = Page.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
put :update, {:id => page.to_param, :page => { }}, valid_session
assigns(:page).should eq(page)
end
it "re-renders the 'edit' template" do
page = Page.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
put :update, {:id => page.to_param, :page => { }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested page" do
page = Page.create! valid_attributes
expect {
delete :destroy, {:id => page.to_param}, valid_session
}.to change(Page, :count).by(-1)
end
it "redirects to the pages list" do
page = Page.create! valid_attributes
delete :destroy, {:id => page.to_param}, valid_session
response.should redirect_to(pages_url)
end
end
end
Errors:
Failures:
1) PagesController GET index assigns all pages as #pages
Failure/Error: assigns(:pages).should eq([page])
NoMethodError:
undefined method `should' for #<Array:0x007fb986c42948>
# ./spec/controllers/pages_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
2) PagesController POST create with valid params creates a new Page
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
# ./spec/controllers/pages_controller_spec.rb:53:in `block (4 levels) in <top (required)>'
3) PagesController POST create with valid params assigns a newly created page as #page
Failure/Error: assigns(:page).should be_a(Page)
NoMethodError:
undefined method `should' for nil:NilClass
# ./spec/controllers/pages_controller_spec.rb:60:in `block (4 levels) in <top (required)>'
4) PagesController POST create with valid params redirects to the created page
Failure/Error: response.should redirect_to(Page.last)
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb987232c68>
# ./spec/controllers/pages_controller_spec.rb:66:in `block (4 levels) in <top (required)>'
5) PagesController POST create with invalid params assigns a newly created but unsaved page as #page
Failure/Error: assigns(:page).should be_a_new(Page)
NoMethodError:
undefined method `should' for nil:NilClass
# ./spec/controllers/pages_controller_spec.rb:75:in `block (4 levels) in <top (required)>'
6) PagesController POST create with invalid params re-renders the 'new' template
Failure/Error: response.should render_template("new")
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb986e1d1f0>
# ./spec/controllers/pages_controller_spec.rb:82:in `block (4 levels) in <top (required)>'
7) PagesController GET new assigns a new page as #page
Failure/Error: assigns(:page).should be_a_new(Page)
NoMethodError:
undefined method `should' for #<Page:0x007fb982404708>
# ./spec/controllers/pages_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
8) PagesController PUT update with valid params updates the requested page
Failure/Error: put :update, {:id => page.to_param, :page => { "these" => "params" }}, valid_session
#<Page:0x007fb9872517f8> received :update_attributes with unexpected arguments
expected: ({"these"=>"params"})
got: ({})
# ./app/controllers/pages_controller.rb:61:in `block in update'
# ./app/controllers/pages_controller.rb:60:in `update'
# ./spec/controllers/pages_controller_spec.rb:96:in `block (4 levels) in <top (required)>'
9) PagesController PUT update with valid params assigns the requested page as #page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb981579a30>
# ./spec/controllers/pages_controller_spec.rb:102:in `block (4 levels) in <top (required)>'
10) PagesController PUT update with valid params redirects to the page
Failure/Error: response.should redirect_to(page)
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb9827b7350>
# ./spec/controllers/pages_controller_spec.rb:108:in `block (4 levels) in <top (required)>'
11) PagesController PUT update with invalid params assigns the page as #page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb987444a88>
# ./spec/controllers/pages_controller_spec.rb:118:in `block (4 levels) in <top (required)>'
12) PagesController PUT update with invalid params re-renders the 'edit' template
Failure/Error: response.should render_template("edit")
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb982e75750>
# ./spec/controllers/pages_controller_spec.rb:126:in `block (4 levels) in <top (required)>'
13) PagesController GET show assigns the requested page as #page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb9830f3a68>
# ./spec/controllers/pages_controller_spec.rb:31:in `block (3 levels) in <top (required)>'
14) PagesController DELETE destroy redirects to the pages list
Failure/Error: response.should redirect_to(pages_url)
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb988100e98>
# ./spec/controllers/pages_controller_spec.rb:142:in `block (3 levels) in <top (required)>'
15) PagesController GET edit assigns the requested page as #page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb9872585f8>
# ./spec/controllers/pages_controller_spec.rb:46:in `block (3 levels) in <top (required)>'
RSpec offers two ways of writing expectations:
should style 1.should == 1
expect style expect(1).to eq(1)
Your spec_helper is configured to use the latter. Change the syntax config to: c.syntax = :should
Reference: https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration
I'm currently going through the awesome Rails Tutorial, and after I did a git reset to return to a previous commit, something broke to my database and all of a sudden I get 5 failures when I run rspec.
Failures:
1) UsersController Get 'show' should be successfull
Failure/Error: get :show, :id => #user
ActionView::Template::Error:
undefined method `gravatar_for' for #<#<Class:0xaadc884>:0xaad9990>
# ./app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb__463664834_89565070__435144589'
# ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
2) UsersController Get 'show' should find the right user
Failure/Error: get :show, :id => #user
ActionView::Template::Error:
undefined method `gravatar_for' for #<#<Class:0xaadc884>:0xa820ca8>
# ./app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb__463664834_89565070__435144589'
# ./spec/controllers/users_controller_spec.rb:18:in `block (3 levels) in <top (required)>'
3) UsersController Get 'show' should have the right title
Failure/Error: get :show, :id => #user
ActionView::Template::Error:
undefined method `gravatar_for' for #<#<Class:0xaadc884>:0x9f0e7b4>
# ./app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb__463664834_89565070__435144589'
# ./spec/controllers/users_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
4) UsersController Get 'show' should include the user's name
Failure/Error: get :show, :id => #user
ActionView::Template::Error:
undefined method `gravatar_for' for #<#<Class:0xaadc884>:0xb930cc8>
# ./app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb__463664834_89565070__435144589'
# ./spec/controllers/users_controller_spec.rb:28:in `block (3 levels) in <top (required)>'
5) UsersController Get 'show' should have a profile image
Failure/Error: get :show, :id => #user
ActionView::Template::Error:
undefined method `gravatar_for' for #<#<Class:0xaadc884>:0xb9ade94>
# ./app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb__463664834_89565070__435144589'
# ./spec/controllers/users_controller_spec.rb:33:in `block (3 levels) in <top (required)>'
I'm sure this easy to fix, but I honestly dont know where to even look at. Can anyone help?
This is your problem:
undefined method `gravatar_for' for #<#<Class:0xaadc884>:0xb9ade94>
You need to make sure the gravatar is specificed in your profile model.
You are calling gravatar_for, which is unknown, line 5 of: ./app/views/users/show.html.erb
Even if this is already an answer, please provide details so that we fix it.
I'm pretty sure you forgot to add the method to your User model.