I have got the error when i have written
RSpec.configure do |c|
c.use_transactional_fixtures = true
c.include Capybara::DSL
end
NoMethodError Exception: undefined method `use_transactional_fixtures
You may not have installed the
gem 'rspec-rails'
or maybe you didn't require it on your specs as
require 'rspec/rails'
use_transaction_fixtures is part of the rspec-rails gem here:
https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/fixture_support.rb#L31
Hope this helps you
Regards
ED
You should write this code in rspec.rb file
Related
I'm using the Mocha gem in a Rails project to make stubs and bypass some live API calls.
I'm conforming to the documentation but keep getting an error.
In Gemfile (I use Bundler) I have:
gem 'minitest'
gem 'minitest-rails'
gem 'mocha', :require => false
In test_helper.rb:
require 'minitest'
require 'mocha/mini_test' # This is the last require statement
In product_controller_test.rb:
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
setup do
Product.any_instance.stubs({
manufacturer: "Acme"
})
end
end
The output is always NoMethodError: undefined method 'any_instance' for #<Class:blah>.
I have another Rails project where I can use any_instance successfully, so I've tried resolving by replicating the gems, require statements, etc. but with no success. I've also tried different permutations like require 'minitest/autorun', require "minitest/rails', and require 'mocha/setup'/require 'mocha', etc.
I'm on Rails 4.1.1 and using MiniTest 5.3.3 and Mocha 1.0.0.
I am getting the following error in my rspec code
undefined local variable or method `render'
here is my code:
require 'spec_helper'
describe "messages/show.html.erb" do
it "displays the text attribute of the message" do
render
rendered.should contain("Hello world!")
end
end
this is from the book : The RSpec book
below is what i added to Gemfile.
group :development , :test do
gem "rspec-rails", ">= 2.0.0"
gem "webrat", ">= 0.7.2"
end
I have not added the file show.html.erb to the views/messages but i should be getting another error , not this
The strange thing is that this worked on my machine some time before. I deleted the project and created another one and now it just wouldn't work
I had issued these commands after editing the gemfile
bundle install
script/rails generate rspec:install
rake db:migrate
For me the answer was to change
describe "messages/show.html.erb" do
to
describe "messages/show.html.erb", type: :view do
If your show.html.erb_spec.rb file contains:
require 'spec_helper.rb'
try changing it to:
require 'rails_helper.rb'
Explanation:
https://www.relishapp.com/rspec/rspec-rails/docs/upgrade
It looks like you're missing a closing quote on the first line of your code:
require 'spec_helper
you could change your test like following:
require 'spec_helper'
describe "messages/show.html.erb" do
it "displays the text attribute of the message" do
FactoryGirl.create(:message) # or an other test data creator gem
visit "/messages/1"
page.should have_content("Hello world!")
end
end
Using Rails 3.2.2, I just installed rspec and tried my first test:
require 'spec_helper'
describe "Mains" do
describe "GET index" do
it "gets the page which has the proper content" do
get 'index.html'
response.status.should be(200)
response.body.should include("one")
end
end
end
When I run the spec I get
Failures:
1) Mains GET index gets the page which has the proper content
Failure/Error: response.body.should include("one")
NoMethodError:
undefined method `chomp' for ["one"]:Array
This comes strait out of Ryan Bates' Railscast 257, so I'm a bit lost
thanks in advance,
[EDIT] The issue is corrected in rspec-expectations, on master branch. You can fix it by using this in your Gemfile:
gem 'rspec-rails', '~> 2.9'
gem 'rspec-expectations', :git => "https://github.com/rspec/rspec-expectations.git", :branch => 'master'
I encountered the same issue on Rails 3.0.12 with rspec 2.9.0. Reverting to rspec 2.8.0 / rspec-rails 2.8.1 did the trick.
I raised an issue on Github, rspec/rspec-expectations, we'll see if it's really a bug.
For reference, this is the step definition:
Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
current_email.default_part_body.to_s.should include(text)
end
... the step in the feature:
step %{I should see "changer de mot de passe" in the email body}
... which was raising:
undefined method `chomp' for ["changer de mot de passe"]:Array (NoMethodError)
./features/step_definitions/email_steps.rb:110:in `/^(?:I|they) should see "([^"]*?)" in the email body$/'
I have upgraded from Rails 3.0.7 to Rails 3.0.9 and I am now getting the following error in my config/application.rb:
/Library/Ruby/Gems/1.8/gems/railties-3.1.0/lib/rails/railtie/configuration.rb:78:in `method_missing': undefined method `active_record' for #<Rails::Application::Configuration:0x10107a830> (NoMethodError)
from /Users/mathias/ruby/myapp/config/application.rb:47
On line 47 of application.rb I have
config.active_record.schema_format = :sql
It seems that
config.active_record
is not found in Rails 3.0.9 where this worked in 3.0.7. This is regardless of which method I call on config.active_record. When I comment out any usage of config.active_record, the same occurs with config.active_mailer.
And I do a require "rails/all" in the application.rb
Any help appreciated.
Got this to work with 3.1.0 through the steps outlined here. The key bit that made most of my problems disappear was to change boot.rb to
require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']
ruby-1.9.2-p180 :007 > Factory.define :user do |user|
ruby-1.9.2-p180 :008 > user.email "user#example.com"
ruby-1.9.2-p180 :009?> user.password "foobar"
ruby-1.9.2-p180 :010?> user.password_confirmation "foobar"
ruby-1.9.2-p180 :011?> end
NameError: uninitialized constant Factory
My Gemfile:
group :test do
gem "rspec-rails"
gem 'webrat', '0.7.1'
gem 'spork', '0.9.0.rc4'
gem 'factory_girl_rails'
end
Even tough it seems I have everything as it should, I keep getting that error. I also have factories.rb created.
Thanks
I suppose you try in console in development environment. But you add the Factory gem only in test environment.
If you want access to Factory_girl in development use in your Gemfile :
group :test, :development do
gem 'factory_girl_rails'
end
Or if you want test your Factory launch your console in test environment :
rails c test
We had a similar problem on our end, rake spec seemed to be randomly failing with the error uninitialized constant FactoryGirl. The error was random -> coming and going. We went back half a dozen git commits to try and resolve it. In the end it was a silly mistake.
The fundamental problem is RAILS_ENV is set to development. It needs to be set to test when you run rake spec.
Address by:
Making certain that we are running rake spec in the RAILS_ENV test environment and its exported/sourced properly. To never confuse our environemnts, we modified zsh $RPROMPT env variable to show the current env.
export RPROMPT="[%{$fg_no_bold[yellow]%}$RAILS_ENV%{$reset_color%}]"
Require FactoryGirl in the spec ruby files gave a much better error message. At least rspec would run vs just fail outright this way when the environment was wrong. We also updated our gemfile to make sure factory_girl_rails and factory_girl were loaded both for development and testing.
Now we just run rpsec using the gem guard in a dedicated terminal with the proper RAILS_ENV set.
Its one of those gotchas.
We're running Ruby 1.9.3, Rails 3.2.9, Rspec 2.12, factory_girl 4.1.
I also ran into this while I was doing the Hartl tutorial.
If you are using "Spork" to speed up your tests and it is running in the background when you add the FactoryGirl code, you will need to stop it and restart it.
You should also change Factory.define to FactoryGirl.define, like in this example
FactoryGirl.define do
factory :user do
name 'John Doe'
date_of_birth { 21.years.ago }
end
end
from the factory_girl documentation