How do I test a non-ActiveRecord model class in Rails? The code that works in the console does not seem to translate to the test suite. Methods are not available and the whole thing just doesn't seem to work!
class CalendarEvent
def test
'Hello World'
end
end
When I fire up the console, this works:
irb> cal_event = CalendarEvent.new
=> #<CalendarEvent:0x007fb5e3ee9fd0>
irb> cal_event.test
=> "Hello World"
However, when I write a test it seems the model is not loading. None of the functions are available.
class CalendarEvent < ActiveSupport::TestCase
include TestApiHelperPackage
test 'validate hello world' do
cal_event = CalendarEvent.new
assert_equal cal_event.test, 'Hello world'
end
end
Seems like it isn't grabbing the model. Do I have to not inherit from ActiveSupport::TestCase ?
I am surprised that it even runs. You declare the class CalendarEvent in the test suit. Which obviously has no test method. Why would you name it the exact same way as the tested class? Just give it another name:
# ⇓⇓⇓⇓ HERE
class CalendarEventTest < ActiveSupport::TestCase
include TestApiHelperPackage
test 'validate hello world' do
cal_event = CalendarEvent.new
assert_equal cal_event.test, 'Hello world'
end
end
And everything should go fine. Possible you want to require 'calender_event' in your test_heler.rb as well.
Related
I simply want to run a method in my test and see if it works.
I tried the following line of code in my testing class:
UserPostcodesImport.add_postcodes_from_csv
My user_postcodes_import_test.rb:
require "test_helper"
require "user_postcodes_import"
class UserPostcodesImportTest < ActiveSupport::TestCase
it "works" do
UserPostcodesImport.add_postcodes_from_csv
end
end
My user_postcodes_import:
class UserPostcodesImport
class << self
def add_postcodes_from_csv
puts "it works"
end
end
end
I expect the console to print "it works" but it prints the error:
NoMethodError: undefined method `add_postcodes_from_csv'
So testing doesn't really work like that. What you need to do in this case is take a look at the test calls and do something like this
test "the truth" do
assert true
end
so you might have
class UserPostcodesImportTest < ActiveSupport::TestCase
it "works" do
test_string = UserPostcodesImport.add_postcodes_from_csv
assert !test_string.blank?
end
end
If you're using rspec, it might look like this:
class UserPostcodesImportTest < ActiveSupport::TestCase
{subject = UserPostcodesImport}
it "works" do
expect (subject.add_postcodes_from_csv).to_not be_nil
end
end
something like that...check rspecs syntax here: https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
The critical part of that is the assert, which is basically what triggers the test to run. You're asking "when I do THIS, does it return true?"
I'd start by looking here: https://guides.rubyonrails.org/testing.html in order to get a better sense of testing best practices.
I am trying to unit test a Plain Old Ruby Object that has a method which calls a class method on a Rails model. The Rails app is quite large (10s of seconds to load) so I'd prefer to avoid loading all of Rails to do my unit test which should run in under 1s.
Example:
class Foo
def bar
SomeRailsModel.quxo(3)
end
end
RSpec.describe Foo do
let(:instance) { Foo.new }
it 'calls quxo on SomeRailsModel' do
expect(SomeRailsModel).to receive(:quxo)
instance.bar
end
end
The problem here is that I need to require 'rails_helper' to load up Rails in order for app/models/some_rails_model to be available. This leads to slow unit tests due to Rails dependency.
I've tried defining the constant locally and then using regular spec_helper which kind of works.
Example:
RSpec.describe Foo do
let(:instance) { Foo.new }
SomeRailsModel = Object.new unless Kernel.const_defined?(:SomeRailsModel)
it 'calls quxo on SomeRailsModel' do
expect(SomeRailsModel).to receive(:quxo)
instance.bar
end
end
This code lets me avoid loading all of Rails and executes very fast. Unfortunately, by default (and I like this) RSpec treats the constant as a partial double and complains that my SomeRailsModel constant doesn't respond to the quxo message. Verifying doubles are nice and I'd like to keep that safety harness. I can individually disable the verification by wrapping it in a special block defined by RSpec.
Finally, the question. What is the recommended way to have fast unit tests on POROs that use Rails models without requiring all of Rails while also keeping verifying doubles functionality enabled? Is there a way to create a "slim" rails_helper that can just load app/models and the minimal subset of ActiveRecord to make the verification work?
After noodling a few ideas with colleagues, here is the concensus solution:
class Foo
def bar
SomeRailsModel.quxo(3)
end
end
require 'spec_helper' # all we need!
RSpec.describe Foo do
let(:instance) { Foo.new }
let(:stubbed_model) do
unless Kernel.const_defined?("::SomeRailsModel")
Class.new { def self.quxo(*); end }
else
SomeRailsModel
end
end
before { stub_const("SomeRailsModel", stubbed_model) }
it 'calls quxo on SomeRailsModel' do
expect(stubbed_model).to receive(:quxo)
instance.bar
end
end
When run locally, we'll check to see if the model class has already been defined. If it has, use it since we've already paid the price to load that file. If it isn't, then create an anonymous class that implements the interface under test. Use stub_const to stub in either the anonymous class or the real deal.
For local tests, this will be very fast. For tests run on a CI server, we'll detect that the model was already loaded and preferentially use it. We get automatic double method verification too in all cases.
If the real Rails model interface changes but the anonymous class falls behind, a CI run will catch it (or an integration test will catch it).
UPDATE:
We will probably DRY this up a bit with a helper method in spec_helper.rb. Such as:
def model_const_stub(name, &blk)
klass = unless Kernel.const_defined?('::' + name.to_s)
Class.new(&blk)
else
Kernel.const_get(name.to_s)
end
stub_const(name.to_s, klass)
klass
end
# DRYer!
let(:model) do
model_const_stub('SomeRailsModel') do
def self.quxo(*); end
end
end
Probably not the final version but this gives a flavor of our direction.
Trying to test a module. It works when executed in rails console, but not when written as a test. Suppose the following:
MyModel
a) has_many :my_other_model
MyOtherModel
a) belongs to :my_model
Module example:
module MyModule
def self.doit
mine = MyModel.first
mine.my_other_models.create!(attribute: 'Me')
end
end
Now test:
require 'test_helper'
class MyModuleTest < ActiveSupport::TestCase
test "should work" do
assert MyModule.doit
end
end
Returns:
NoMethodError: NoMethodError: undefined method `my_other_models' for nil:NilClass
Now try the same thing in the console:
rails c
MyModule.doit
Works just fine. But why not as a test?
Your test database is empty when you run this test, so calling MyModel.first is going to return nil, then you try to chain an unknown method to nil. What you'll probably want for your test suite is a fixture, which is just sample data. For now, you you can just create the first instance to get the test to work.
test "should work" do
MyModel.create #assuming the model is not validated
assert MyModule.doit
end
You could also refactor your module. Adding if mine will only try to create the other models if mine is not nil. That would get the test to pass, but negates the purpose of your test.
def self.doit
mine = MyModel.first
mine.my_other_models.create!(attribute: 'Me') if mine
end
I want to add a test for a one-off task, but once it runs, in the future it should not get run as part of the full suite.
You can add a guard clause on the test method, or the test class for whether it should exist. For example:
def test_greet
greeter = HelloWorld.new
assert_equal "Hello world!", greeter.greet
end if ENV["ONEOFF"]
Or, for the whole test class:
class TestHello < Minitest::Test
def test_greet
greeter = HelloWorld.new
assert_equal "Hello world!", greeter.greet
end
end if ENV["ONEOFF"]
Then to have these run just define the ONEOFF environment variable when you run your test.
If you're using Rspec, checkout Rspec filtering. That may be what you want: https://www.relishapp.com/rspec/rspec-core/v/2-14/docs/filtering/inclusion-filters
I use a decorator module that get's included in a model instance (through the "extends" method). So for example :
module Decorator
def foo
end
end
class Model < ActiveRecord::Base
end
class ModelsController < ApplicationController
def bar
#model = Model.find(params[:id])
#model.extend(Decorator)
#model.foo
end
end
Then I would like in the tests to do the following (using Mocha) :
test "bar" do
Model.any_instance.expects(:foo).returns("bar")
get :bar
end
Is this possible somehow, or do you have in mind any other way to get this functionality???
Just an Assumption Note: I will assume that your Decorator foo method returns "bar" which is not shown in the code that you sent. If I do not assume this, then expectations will fail anyway because the method returns nil and not "bar".
Assuming as above, I have tried the whole story as you have it with a bare brand new rails application and I have realized that this cannot be done. This is because the method 'foo' is not attached to class Model when the expects method is called in your test.
I came to this conclusion trying to follow the stack of called methods while in expects. expects calls stubs in Mocha::Central, which calls stubs in Mocha::ClassMethod, which calls *hide_original_method* in Mocha::AnyInstanceMethod. There, *hide_original_method* does not find any method to hide and does nothing. Then Model.foo method is not aliased to the stubbed mocha method, that should be called to implement your mocha expectation, but the actual Model.foo method is called, the one that you dynamically attach to your Model instance inside your controller.
My answer is that it is not possible to do it.
It works (confirmed in a test application with render :text)
I usually include decorators (instead of extending them at runtime) and I avoid any_instance since it's considered bad practice (I mock find instead).
module Decorators
module Test
def foo
"foo"
end
end
end
class MoufesController < ApplicationController
def bar
#moufa = Moufa.first
#moufa.extend(Decorators::Test)
render :text => #moufa.foo
end
end
require 'test_helper'
class MoufesControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "bar" do
m = Moufa.first
Moufa.expects(:find).returns(m)
m.expects(:foo).returns("foobar")
get :bar, {:id => 32}
assert_equal #response.body, "foobar"
end
end
Ok, now I understand. You want to stub out a call to an external service. Interesting that mocha doesn't work with extend this way. Besides what is mentioned above, it seems to be because the stubbed methods are defined on the singleton class, not the module, so don't get mixed in.
Why not something like this?
test "bar" do
Decorator = Module.new{ def foo; 'foo'; end }
get :bar
end
If you'd rather not get the warnings about Decorator already being defined -- which is a hint that there's some coupling going on anyway -- you can inject it:
class ModelsController < ApplicationController
class << self
attr_writer :decorator_class
def decorator_class; #decorator_class ||= Decorator; end
end
def bar
#model = Model.find(params[:id])
#model.extend(self.class.decorator_class)
#model.foo
end
end
which makes the test like:
test "bar" do
dummy = Module.new{ def foo; 'foo'; end }
ModelsController.decorator_class = dummy
get :bar
end
Of course, if you have a more complex situation, with multiple decorators, or decorators defining multiple methods, this may not work for you.
But I think it is better than stubbing the find. You generally don't want to stub your models in an integration test.
One minor change if you want to test the return value of :bar -
test "bar" do
Model.any_instance.expects(:foo).returns("bar")
assert_equal "bar", get(:bar)
end
But if you are just testing that a model instance has the decorator method(s), do you really need to test for that? It seems like you are testing Object#extend in that case.
If you want to test the behavior of #model.foo, you don't need to do that in an integration test - that's the advantage of the decorator, you can then test it in isolation like
x = Object.new.extend(Decorator)
#.... assert something about x.foo ...
Mocking in integration tests is usually a code smell, in my experience.