I'm using Minitest spec in a rails project and I want to use must as an alias for must_be and wont as an alias for wont_be
in April 2015 Chris Kottom gave an example of this by doing this:
module Minitest::Expectations
alias_method :must, :must_be
end
But when I try the same it doesn't work. I put that code before my test class and this test:
it 'uses the alias' do
user = User.take
_(user).must(:valid?)
end
gives this error:
Minitest::UnexpectedError: NoMethodError: undefined method `valid?' for #<Minitest::Expectation:0x0055b830595608>
(when I change must to must_be the test passes)
What am I doing wrong?
Related
I'm running into this error when upgrading from Rails 6.0.3 to 6.1:
NoMethodError:
undefined method `assert_nothing_raised' for #<RSpec::ExampleGroups::EmailJob:0x00005572d8a00758>
Did you mean? assert_raises
This happens every time a test calls perform_enqueued_jobs.
I'm using RSpec 3.9.
Apparently assert_nothing_raised is a helper method defined by ActiveSupport. I was able to solve this issue by explicitly including the helper in spec/rails_helper.rb:
RSpec.configure do |config|
config.include(ActiveSupport::Testing::Assertions)
# ...
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?
Is there a way to include the config_for settings in rspec tests? I have the following settings configured in config/initialisers/settings.rb:
SETTINGS = Rails.application.config_for(:settings)
but they aren't autoloaded in rspec and it triggers the following:
Failure/Error: generator = SETTINGS['my_strategy'].constantize.new(file)
NoMethodError:
undefined method `constantize' for nil:NilClass
As it isn't a module or class, I can't include it in the normal way and doing something as:
config.include Rails.application, type: :controller
Is in my opinion, not the proper thing to do.
After some digging I couldn't find a rspec helper which can load the config_for settings. I solved this by doing the following:
before :all do
SETTINGS['my_strategy'] = 'StrategyClass'
end
Which solved the loading of the constant.
I generated the following scaffold (using Ruby 2.2.0, rails 4.1.8, postgres):
rails g scaffold Test user:references text:references data:hstore
In my test_spec.rb:
require 'rails_helper'
RSpec.describe Test, :type => :model do
describe 'User generates a test' do
before do
#text = create(:text, content: "Cats eat mice")
#user = create(:user)
#test = create(:test)
end
...
When I run rspec the test fails with the following message:
Failure/Error: #test = create(:test)
NoMethodError:
undefined method `new' for Test:Module
# ./spec/models/test_spec.rb:8:in `block (3 levels) in <top (required)>'
When I test other models (user, text) everything works well, only the Test model fails. Calling Test.create(...) in rspec file also fails. Creating new test in rails console works. Any ideas how to fix this?
With the default configuration, Rails defines the module constant Test, so Ruby doesn't autoload your test.rb file when FactoryGirl does a Test.new as part of your :test factory.
You can install Rails without the Test infrastructure by using the -T switch, in which case it won't define the Test module and you should be fine.
If Rails is already configured, you can put the following in your rails_helper.rb file to remove the Test constant and you should be ok as well:
Object.send(:remove_const, :Test)
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