Cannot use assert_difference and active_support/testing/assertions methods in PORO classes
Closed this issue · 3 comments
Using minitest-rails (2.2.0)
When I do a assert_difference, I get a
NoMethodError: undefined method
assert_difference' for #<#<Class:`
The class I am testing is a plain ruby class. It is not inheriting from ActiveRecord::Base, or controller.
All gets fixed if I add to my test_helper.rb
# test_helper.rb
include ActiveSupport::Testing::Assertions
This may be expected behaviour, I don't know.
Classes:
# app/services/my_service.rb
class MyService
# do stuff
end
# test/services/my_service_test.rb
describe MyService do
it 'should allow assert_difference' do
assert_difference stuff
end
end
The rails assertions are added to ActiveSupport::TestCase. You need to make sure your test is inheriting from AS::TC. You can do this by inheriting from it directly, or if you are sing the spec DSL describe a class that inherits from ActiveRecord. If your class does not inherit from AR, you can use the :model
additional description:
describe MyService, :model do
# tests here
end
Ah, ok. Thanks for the explanation. Expected behaviour then, closing.
This should be documented, I think it's a common use case, for example, to test some POROs defined in lib/
but that rely on fixtures.
@blowmage I can do a PR for this if you think it's worth.