Rails /lib use in fixtures - ruby-on-rails

I have some common code that I have abstracted to a module in my /lib directory:
module ExampleModule
def self.do_something
# return a value
end
end
I need to use this method in my test fixtures, however I am getting the following error when I run tests:
Error:
SomeTest#test_that_something:
NoMethodError: undefined method `do_something' for ExampleModule:Module
test/fixtures/model_name.yml:4:in `get_binding'
How can I access this method in the test fixtures? I can fix the error by requiring the module in other parts of the app that are loaded during test, but how would I require this module if it was needed specifically in the fixture file here? Would I need to require it in any test using the fixture?

Related

How do I make including a custom module in an RSpec test work?

I am trying to include a custom module into an RSpec test. However, I keep getting errors telling me it can't find my module, it says NameError: uninitialized constant AuthHelper.
I have been trying to follow this https://stackoverflow.com/a/68669816/5568244
I didn't know where to put the the module so I created a folder called specs/support/ and a file called auth_helper.rb in it and put module AuthHelper ...etc... end in that
I have tried simply including it in the test
spec/requests/topics_specs.rb
require 'rails_helper'
include AuthHelper
which i expected to be able to just automatically find the module in the support folder but it didn't. Should it? Does RSpec look in specific places?
I also tried to set up the rails_helper.rb file to include it
RSpec.configure do |config|
config.include(AuthHelper, :type => :request)
end
This still didn't work.
What am i missing? How should this work?

rspec + rails_admin testing custom rails_admin action in /lib gives error

My rspec test were executing fine before I created a custom rails_admin action in lib folder.
Custom action for creating a Dashboard looks like this: /lib/rails_admin.rb
module RailsAdmin
module Config
module Actions
class Dashboard < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
register_instance_option :root? do
true
end
end
end
end
end
It gives the desired results i.e: new dashboard.
But now when I run rspec test (any test) it gives following error:
Bundler::GemRequireError:
There was an error while trying to load the gem 'rails_admin'.
Gem Load Error is: uninitialized constant
RailsAdmin::Config::Actions::Base
Before running any test rspec loads all files from lib folder but can't find RailsAdmin for some reason.
To get rid of the error I tried to manually include rails_admin files
require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'
module RailsAdmin
module Config
module Actions
class Dashboard < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
register_instance_option :root? do
true
end
end
end
end
end
It doesn't fix the problem just moves the error to the next level and gives following error:
Failure/Error: require File.expand_path('../../config/environment',
FILE)
NoMethodError: undefined method config' for RailsAdmin:Module
./config/initializers/rails_admin.rb:1:in'
Where rails_admin.rb first line is simply:
RailsAdmin.config do |config|
Why rspec isn't able to load the rails_admin gem correctly?
Although I have no clue on why this works, I found a working solution :
instead of having lib/rails_admin.rb, I created a config subfolder and an actions subfolder in it, then placed my rails_admin.rb file inside.
I ended up with the following tree : lib/config/actions/rails_admin.rb. You also need to change the path of this file in your config/initializers/rails_admin.rb :
In my case I had require Rails.root.join('lib', 'rails_admin.rb') and changed it to require Rails.root.join('lib', 'config', 'actions', 'rails_admin.rb').
Then to run bundle exec rspec works as expected.

Rails autoloading behaves strangely in RSpec with modules and subclasses with spork

In my Rails app I've added the following files:
app/models/baz.rb
lib/presenters/foo_presenter.rb
lib/presenters/foo_presenter/bar.rb
spec/models/baz_spec.rb
spec/lib/presenters/foo_presenter/bar_spec.rb
The contents of lib/presenters/foo_presenter.rb is something like:
module Presenters
module FooPresenter
def self.render
# do stuff
end
end
end
The contents of lib/presenters/foo_presenter/bar.rb is like:
module Presenters
class FooPresenter::Bar
def baz
# do stuff
end
end
end
The contents of spec/lib/presenters/foo_presenter/bar_spec.rb is like:
require 'spec_helper'
module Presenters::FooPresenter
describe Bar do
# some tests
end
end
Then I have a spec file in spec/models/baz_spec.rb:
require 'spec_helper'
describe Baz do
it 'works' do
Presenters::FooPresenter.render
end
end
(The contents of app/models/baz.rb is not relevant to this issue)
The problem is when I run rspec spec/models/baz_spec.rb it works fine without spork, but when spork is running, I get an error like:
NameError: undefined method `render' for Presenters::FooPresenter:Module
I traced through the code a bit and noticed that when rspec loads spec/lib/presenters/foo_presenter/bar_spec.rb it causes Rails to autoload lib/presenters/foo_presenter/bar.rb and so at that point Presenters::FooPresenter::Bar is loaded, but then when baz_spec.rb runs, lib/presenters/foo_presenter.rb has never been loaded and thus the exception. But this only happens if spork is running. The quick fix was to require 'foo_presenter' in a file in config/initializers, but is there a cleaner solution that doesn't need the explicit require? My guess is the issue here is that Rails doesn't autoload lib/presenters/foo_presenter.rb because Presenters::FooPresenter has already been defined by bar_spec.rb.
A co-worker and I were faced with this problem today and we eventually found we needed Spork to reload the classes on every run. We used the each_run() method to do this:
Spork.each_run do
Dir[Rails.root.join("app/classes/**/*.rb")].each {|f| require f}
end

undefined method in ActionView (rspec)

I have a method in my application helper but for some reason it isn't being found when I run my rspec tests. Here's the error:
Failure/Error: helper.chart_series(user).should =~ /y: 100/
undefined method `ab_variant' for #<ActionView::Base:0x102b12d78>
# ./app/helpers/sidebar_helper.rb:17:in `chart_series'
# ./spec/helpers/sidebar_helper_spec.rb:35
How do i make sure ab_variant (within application helper) can be found by my rspec tests?
Your helpers defined in app/helpers aren't loaded in rspec by default. You have to re-create them.
So create a file and call it a name you like, for ex. "application_helper.rb" and place it inside spec/support. This will be included automatically and will be available in your specs by this file in spec/spec_helper.rb:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

performance testing with devise authentication

How can I run performance tests with devise. Using the Devise docs I have this in my test_helper.rb
class ActionController::TestCase
include Devise::TestHelpers
end
From Functional testing with Rails and Devise. What to put in my fixtures?, I have this in my performance test:
require 'test_helper'
require 'rails/performance_test_help'
class EditorTest < ActionDispatch::PerformanceTest
def test_create
#user = users(:one)
sign_in #user
get 'documents/new/1'
end
end
I get the following error
NoMethodError: undefined method `sign_in' for #<EditorTest:0xb6bc0654 ...>
/test/performance/editor_test.rb:9:in `test_create'
How do I properly include the Devise TestHelpers in a performance test?
Thank you!
[edit]
This works as a functional test.
[edit]
After including the devise helper in ActionDispatch::PerformanceTest and running the test with ruby -d, here is the bottom of the debug output:
/usr/lib/ruby/gems/1.8/gems/devise-1.1.rc2/lib/devise/test_helpers.rb:
53: warning: instance variable #request not initialized
Exception `NoMethodError' at /usr/lib/ruby/gems/1.8/gems/
activesupport-3.0.3/lib/active_support/whiny_nil.rb:48 - undefined
method `env' for nil:NilClass
EEditorTest#test_create (0 ms warmup)
/usr/lib/ruby/gems/1.8/gems/devise-1.1.rc2/lib/devise/test_helpers.rb:
53: warning: instance variable #request not initialized
Exception `NoMethodError' at /usr/lib/ruby/gems/1.8/gems/
activesupport-3.0.3/lib/active_support/whiny_nil.rb:48 - undefined
method `env' for nil:NilClass
E process_time: 0 ms
Exception `Errno::EEXIST' at /usr/lib/ruby/1.8/fileutils.rb:243 -
File
exists - tmp/performance
Exception Errno::EEXIST' at /usr/lib/ruby/1.8/fileutils.rb:243 -
File
exists - tmp/performance
ExceptionErrno::EEXIST' at /usr/lib/ruby/1.8/fileutils.rb:243 -
File
exists - tmp/performance
The short version of the above error:
NoMethodError: undefined method `env' for nil:NilClass
(Misread question original reply to the wrong question is below.)
Try adding
class ActionDispatch::PerformanceTest
include Devise::TestHelpers
end
to the bottom of your helper file.
(Original reply)
Make sure
class ActionController::TestCase
include Devise::TestHelpers
end
is all the way at the bottom of your helper file. It should NOT be inside the ActiveSupport::TestCase class.
It seems performance tests don't set a request variable, which Devise test helpers try to access. In other words, Devise test helpers won't help you here.
As suggested to you here http://groups.google.com/group/plataformatec-devise/browse_thread/thread/b50bfd8ecb24822c try filling in the sign in form like in integration tests.
This explains how integration tests work, with an example how to sign in: http://guides.rubyonrails.org/testing.html#integration-testing

Resources