Database Cleaner not working in minitest rails - ruby

My Minitest controller tests are working fine if I run them alone using rake minitest:controllers but when I run rake minitest:all then I get validation failed error. It is because email is already used in model tests. I used DatabaseCleaner to clean the database but unable to clean database.
My code for database cleaner:
require "database_cleaner"
DatabaseCleaner.strategy = :transaction
class MiniTest::Rails::ActionController::TestCase
include Devise::TestHelpers
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end

Short answer:
gem install "minitest-around"
Long answer:
before/after or setup/teardown in minitest are NOT hooks as in rspec, therefore you can't have multiple before/after or setup/teardown in minitest, since what they do is just redefining the method.
To solve this issue, you can use minitest-around, which adds support for multiple before/after or setup/teardown and around, simply add the gem to your test group:
# put in your Gemfile
gem 'minitest-around', group: :test
For setting up the database_cleaner, you can have it as you want, following is an example of the setup:
# tests/support/database_cleaner.rb
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
class Minitest::Rails::ActionController::TestCase
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end
And in your test files:
# tests/your/test/file_test.rb
require 'support/database_cleaner'
# assertions here ...
That's it, see the Github for detailed info.

If for whatever reason you don't want to add the 'minitest-around' gem (to have more than one setup and teardown method), you can do this in your test_helper.rb...
require "database_cleaner"
DatabaseCleaner.strategy = :transaction
module AroundEachTest
def before_setup
super
DatabaseCleaner.start
end
def after_teardown
super
DatabaseCleaner.clean
end
end
class Minitest::Test
include AroundEachTest
end

I found my mistake ,may be it helps someone else ..
I should write DatabaseCleaner.start in setup of every model test where setup is defined, as i am overwriting setup method in every test file.

This is why I like Minitest; no fancy DSL to block thinking about how to use Ruby properly.
My setup is as follows:
In test_helper.rb
class MyTest < Minitest::Test
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end
Then I just subclass this in any test that need database cleaning. Note the call super first cleans the db before any subclass-specific setup. The same call to super would need to be included in any subclass teardown method, but this can usually be omitted entirely.
class FooTest < MyTest
def setup
super
#foo = Foo.new(bar: 'whatever')
end
def test_save
#foo.save
assert_equal 1, Foo.count
end
end
If I need to subclass MyTest further (e.g. for integration tests) I include its own setup & teardown methods with calls to super so it goes right up the inheritance tree.

You can DRY up that repetition with this
DatabaseCleaner.strategy = :truncation
class MiniTest::Spec
before :each do
DatabaseCleaner.clean
end
end
This example subclasses the spec runner, but you can pick your test environment of choice.

Related

Unable to override RSpec Sysntax module method

I wanted to override the expect in under Syntax module. So, i have placed the below code into the .config/initializers/syntax.rb file
module RSpec
module Expectations
module Syntax
def enable_expect(syntax_host=::RSpec::Matchers)
return if expect_enabled?(syntax_host)
syntax_host.module_exec do
def expect(value=::RSpec::Expectations::ExpectationTarget::UndefinedValue, &block)
::RSpec::Expectations::ExpectationTarget.for(value, block)
end
end
end
end
end
end
And required this inside the env.rb file.
require_relative '../../.config/initializers/syntax'
This is not overriding the existing method. I'm using RSpec gem 3.2.0
What went wrong with the configuration?
I suggest you to put this override codes in spec/support directory and require it in rails or spec helper instead of putting in initializers.

Access Sinatra settings from Minitest test_helper

I am trying to access a variable defined in Sinatra settings from my test helper with no luck. This is my code:
Main app:
require 'sinatra'
set :foo, 'bar'
# use settings.foo in the routes
tests/test_helper.rb
ENV['RACK_ENV'] = 'test'
require 'minitest/autorun'
require 'rack/test'
module Minitest
class Spec
include Rack::Test::Methods
def app
Sinatra::Application
end
before do
# do something with settings.foo
end
end
end
I have tried Sinatra::Application.settings.foo and also app.settings.foo but none work.
I have also tried adding a helper method like the one below:
lib/helpers/settings_helper.rb
module SettingsHelper
def foo
settings.foo
end
end
helpers SettingsHelper
This works inside app, but again doesn't work inside test_helper. I tried requiring settings_helper.rb in test_helper. Also added an include. None of this worked.
Does anybody know what am I doing wrong?
Happy holidays
Not sure if this will work with a classic style app. But with a modular app you can do MyApp.set :foo, 'bar'. So you could try Sinatra::Application.set :foo, 'bar'
Hope this helps.

Extending Modules from a gem instead of monkey patching

Assuming that "clean" Ruby monkey patching isn't an option, and all gem extension methods need to be completely contained within their own namespace, and given a structure like
module ARubyGem
class GemClassOne
def method1
# instance method to be available when calling extended module
end
def self.method2
# singleton method to also be available in extended module
end
end
end
Then which of these is the better way to extend a gem if the original gem code has some singleton methods that begin with "self"?
module ARubyGemExtension
class GemClassOneExtension < ARubyGem::GemClassOne
def method_to_override
# new code here
end
end
end
vs.
module ARubyGemExtension
include ARubyGem
class GemClassOneExtension
def method_to_override
# new code here
end
end
end
Seems like you would need to go with:
module ARubyGemExtension
class GemClassOneExtension < ARubyGem::GemClassOne
def method_to_override
# new code here
end
end
end
(Remember to namespace GemClassOne.)
I don't know anything about the gem you're using, but unless it's specifically set up for providing methods through include, I think you may not get what you're after.

How to run code before each test case in all tests in MiniTest?

I need to run code before each test in all my tests in MiniTest.
Before I did:
MiniTest::Unit::TestCase.add_setup_hook do
...code to run before each test
end
After I upgraded MiniTest to version 4.7.2 it shows the following error:
undefined method `add_setup_hook' for MiniTest::Unit::TestCase:Class (NoMethodError)
I am using Ruby MRI 2.0.0p0.
SOLUTION
module MyMinitestPlugin
def before_setup
super
# ...code to run before all test cases
end
def after_teardown
# ... code to run after all test cases
super
end
end
class MiniTest::Unit::TestCase
include MyMinitestPlugin
end
add_setup_hook was removed in 4.6.0.
https://github.com/seattlerb/minitest/commit/792a480ebeb32983b9150adae575b7c396e2ae63
Use before_setup instead.
I think that you're looking for the setup() method.
If using MiniTest, you can set that in test/test_helper.rb:
class ActiveSupport::TestCase
...
setup do
...code to run before each test
end
end
Update 2019
Don't write a plugin for this, plugins are intended for gems which extend Minitest functionality, not for test authors.
If you write Minitest Specs, you can do the following instead:
class Minitest::Spec
before :each do
[do stuff]
end
end

How to use RSpec to test a Sinatra application within a gem?

I am writing a gem which includes a Sinatra application that a developer can extend. For example:
# gem code:
require 'sinatra'
module Mygem
class Application < Sinatra::Base
get 'auth/login' {}
get 'auth/logout {}
end
end
# developer code:
require 'mygem'
class DeveloperApp < Mygem::Application
# ..
end
I am also getting started using RSpec. How should I configure RSpec for testing this functionality?
The references above are all informative and useful but mostly rails specific. I found it quite hard to find a simple recipe for a basic test of a modular Sinatra app, so I am hoping this will answer the question for others. Here is a completely bare-bones, small as possible test. This is probably not the only way to do it, but it works well for a modular app:
require 'sinatra'
class Foo < Sinatra::Base
get '/' do
"Hello"
end
end
require 'rack/test'
describe Foo do
include Rack::Test::Methods
def app
Foo.new
end
it "should be testable" do
get '/'
last_response.should be_ok
end
end
Note that there is no need to have the server running when you launch the test (some tutorials I saw implied that you do) - it's not an integration test.
It's actually pretty simple -- just add rspec to your gemfile (then bundle install), and make a directory in your gem called spec/. Once you've done that, add a file spec/spec_helper.rb that contains some configuration for rspec (mostly requiring various files from your library) as well as defining some helper methods for your specs. Then, for each model and controller, make a file called my_model_name_spec.rb or my_controller_name_spec.rb, and do the test there.
Here are some useful resources for getting started with rspec:
Railscasts:
http://railscasts.com/episodes/275-how-i-test
http://railscasts.com/episodes/71-testing-controllers-with-rspec
http://railscasts.com/episodes/157-rspec-matchers-macros/
And for some more advanced (but well-explained) stuff:
http://benscheirman.com/2011/05/dry-up-your-rspec-files-with-subject-let-blocks
Be sure to include the rack-test gem.
You spec helper should have:
require 'rack/test'
require 'foo' # or where ever your app is
# This can go in a helper somewhere
module AppHelper
def app
Foo.new
end
end
RSpec.configure do |config|
config.include Rack::Test::Methods
config.include AppHelper
end
Then, your spec can be as follows:
require 'spec_helper'
# Example app. Delete this example.
class Foo < Sinatra::Base
get '/' do
'Jesse Pinkman'
end
end
describe Foo do
it 'is testable' do
get '/' do
expect(last_response).to be_ok
end
end
end

Resources