I'm trying to use this line in a minitest test that uses capybara, poltergeist, and phantomjs:
bip_select(#gs, :goal_id, Goal.first.name)
This is a helper that best_in_place offers to simulate a user choosing a value from a field. I've read a few questions elsewhere on StackOverflow where other developers who are using RSpec have added this line to their spec_helper.rb file:
config.include BestInPlace::TestHelpers
I've tried adding this line to my test_helper.rb file and I've tried adding it to the test in question. But I'm still getting the error
NoMethodError: undefined method `bip_select' for #<GoalStudentsPoltergeistEditTest:0x00000006d85148>
Thank you in advance for any insight.
To get the method definition available in your integration tests, edit test_helper.rb to include the lines referring to Best in Place (other lines left for context):
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'best_in_place/test_helpers'
# ...
class ActionDispatch::IntegrationTest
include BestInPlace::TestHelpers
end
Related
I have method in my application controller and want to use it everywhere
in my integration specs.
I don't want to add it method in every spec
Currently i use
allow_any_instance_of(ApplicationController).to receive(:set_seo).and_return('seo_text')
but it is inconvenient.
What should i do for it?
In your Rspec config you can configure a before and after block for :
before suite
before all
before each
after each
after all
after suite
https://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks
In that order.
I would suggest:
RSpec.configure do |config|
config.before(:suite) do
allow_any_instance_of(ApplicationController).to receive(:set_seo).and_return('seo_text')
end
end
Edit:
It appears that before(:suite) can cause problems.
If it doesn't work for you use before(:each)
I would create a spec_helper_integration file and put functionality specific to integration specs in there.
You should already have require 'rails_helper' at the top of all your specs. At the top of your integration specs put:
require 'rails_helper'
require 'spec_helper_integration'
Then create a spec_helper_integration.rb file in the same folder as your rails_helper.rb file.
spec_helper_integration:
#I'm taking a guesstimate as to your integration spec configuration, but it's
#likely something like the following line:
#don't also have this in your spec_helper or rails_helper files:
require 'capybara/rails'
#configure your integration specs:
RSpec.configure do |config|
config.before(:each) do
allow_any_instance_of(ApplicationController).to receive(:set_seo).and_return('seo_text')
end
end
It's good practice to isolate code to where it is required only; by doing this, your ApplicationController method stubbing is only activated during the running of your integration specs and not your other specs, such as unit or controller specs, for example.
Moving forward, any further integration-spec-specific code should only be put in your spec_helper_integration file, too.
If I add:
config.include FactoryBot::Syntax::Methods
under
RSpec.configure do |config|
and run rspec, I see this error:
/Users/perry_mac/rails_projects/mymri/spec/spec_helper.rb:21:in `block
in ': uninitialized constant FactoryBot (NameError)
my gemfile.lock can be seen in this pastebin
my gemfile can be seen in this pastebin
If I omit the Rspec.configure statement, my tests all run fine. I'd like to make use of the abbreviated syntax, but am not sure what I am doing wrong here.
Note: FactoryBot was previously called FactoryGirl
Got it.
This link showed me the way.
The required addition should be made in spec/support/factory_bot.rb and it should look like this:
# RSpec
# spec/support/factory_bot.rb
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
Note: FactoryBot was previously called FactoryGirl
You must add this string in file 'spec/RAILS_helper.rb' not in 'spec_helper.rb'
Also make sure to require 'factory_bot' in spec/support/factory_bot.rb
That's what ended up being the issue for me.
Make sure to include require 'support/factory_girl' in spec/rails_helper.rb after require 'rspec/rails'.
I was getting this error after putting it right after require 'spec_helper'.
This answer is compiled and tested from previous comments and factory_bot.
Create spec/support/factory_bot.rb file.
Paste inside spec/support/factory_bot.rb file:
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
Add require 'support/factory_bot.rb' in spec/rails_helper.rb file.
Write tests with pleasure.
I think it's worth mentioning here that, going forward, if using FactoryGirl, you will receive a deprecation message:
The factory_girl gem has been deprecated and has been replaced by factory_bot. Please switch to factory_bot as soon as possible.
Just a note for developers in the future that are trying to use FactoryGirl.
I've got some work to do refactoring a mailer class. I don't want to spend all my time loading rails and waiting to see if tests are passing. I'd like to just not include spec_helper and speed up my tests.
How do I just include ActionMailer?
I tried this:
require 'action_mailer'
but I stil end up with this error:
uninitialized constant ActionMailer (NameError)
Any ideas?
I'm using MiniTest, and the following works fine for me. You may be able to extrapolate from this, or provide some more info in your question.
require 'minitest/autorun'
require 'mocha'
require "minitest-matchers"
require 'action_mailer'
require "email_spec"
require File.expand_path('../../../../app/mailers/my_mailer', __FILE__)
class MyMailerTest < MiniTest::Unit::TestCase
include EmailSpec::Helpers
include EmailSpec::Matchers
def test_it_is_sent_from_me
email = MyMailer.refund_processed(42)
email.must be_delivered_from("me#example.com")
end
end
I create a simpe gem wich include a install generator, generator works fine but now I want to test it using rspec, I foud this gem, and try to test my generator, my spec code is:
require 'genspec'
require 'rosalie'
describe :install_generator do
it "should generate model" do
subject.should generate("message.rb")
end
end
rosalie is the name of may gem, now when I run it I got an error:
/stuff/work/my_projects/rosalie/lib/rosalie/engine.rb:2:in `': uninitialized constant Rosalie::Rails (NameError)
my engine.rb code is:
module Rosalie
class Engine < Rails::Engine
initializer "rosalie.models.messageable" do
ActiveSupport.on_load(:active_record) do
include Rosalie::Models::Messageable
end
end
end
end
anybody can help me with this problem?
You have to load your code before you include it somewhere.
Either require or autoload your main file.
Here is an example from my gem.
You need add these code in your spec_helper.rb, and require the spec_helper in each spec.
require File.expand_path("../dummy/config/environment", __FILE__)
require 'rspec/rails'
When running my specs with rspec & capybara, it can't find capybara's visit method. Is there another initialization step I need to do?
$bundle exec rspec spec
/home/brian/projects/expense_track/expense_track/spec/requests/homepage_spec.rb:6:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xb6572b8> (NoMethodError)
Gemfile:
group :test, :development do
gem "rspec-rails"
gem "capybara"
end
top of my spec_helper.rb:
# 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 'capybara/rspec'
require 'rspec/autorun'
homepage_spec.rb:
require 'spec_helper'
describe "The home page" do
context "home page exists" do
visit "/"
page.should have_content("elephants")
end
end
Just ran into this issue myself.
So the reason for this is there has been a somewhat undocumented change in Capybara. Capybara now makes the assumption that anything using it needs to be in the spec/features folder and it will make the proper assumptions. Anything left in the spec/requests folder will no longer work. Though there are workarounds.
For a context block you can add the parameter :type => :feature and this will fix that issue or you can change the name of a describe method at the beginning of a spec to feature and this should change it as well.
They announced this change in their Google group: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q
Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.
This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814
A few things to note here :
The changes in Capybara 2.0.x are documented here https://github.com/rspec/rspec-rails/blob/master/Capybara.md . There are changes in the spec directory structure : spec/features, spec/controllers, spec/views, spec/helpers, spec/mailers.
load Capybara dsl explicitly inside your spec_helper
require 'capybara/rails'
require 'capybara/rspec'
include Capybara::DSL
This worked for me.
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
RSpec.configure do |config|
config.include Capybara::DSL, :type => :request
end
This enables you to use Capybara's helpers inside spec/requests.
Because RSpec.configure not including capybara DSL in spec_helper.rb
It is an ugly solution, but you can add this to your spec_helper.rb.
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
The git issue for this:
https://github.com/rspec/rspec-rails/issues/503
Unfortunately this workaround doesn't do it for me. I still get
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>
The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
You need to look at the branch '28817499-subscribe'
edit: If i put include Capybara::DSL inside my describe block it works.
but including Capybara::DSL in the global scope is not recommended!
Because I do not know a good way.
For rspec 3 and rails, make sure you are using require "rails_helper", instead of require "spec_helper".
Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.