MiniTest is a fast and lightweight testing library. Here you will learn how to use it in a Rails app for model, helper, and integration testing. The Turn gem is also demonstrated for prettier test output.
I had to use database_cleaner to clean up my testing database before each test:
ruby
require 'database_cleaner'DatabaseCleaner.strategy = :truncationclassMiniTest::Spec
before :eachdoDatabaseCleaner.clean
endend
This does not use transactions, although it should be possible somehow with database_cleaner. They have an RSpec example on their GitHub page which maybe could work.
So there is basically no need to setup all the extra classes, one can just use the rails internal stuff, including the test rake tasks...
@Ryan, is there a reason you created everything from scratch?
Or is it that when adding the minitest gem one has to do that to avoid using the ruby internal minitest?
Great! :-)
Ryan, do you already use MiniTest instead of the "classic" stack around RSpec ect as your standard approach? Or does it depends and differ from project to project?
Are there still some gaps, some gems of the testing-ecosystem (like guard, spork, vcr, jasmine ect) that do not work yet properly with MiniTest?
Or, asked different: for someone who isn't yet a pro in rails-testing, should I invest time in RSpec ect. or MiniTest?
Thanks again for this cool episode!
Very nice episode !
I can't seem to get Simplecov working with Minitest.
The output is of 0% file coverage when i write on top of the minitest_helper.rb file :
ruby
require 'simplecov'SimpleCov.start 'rails'
Has anyone got this to work ?
Thanks a lot for your help.
Pretty awesome. I've been using RSpec for a while and the tests are really slow. I really like how light weight MiniTest is. Hopefully it'll run faster.
Has anyone used Minitest to test associations in their models? I can't seem to find any examples of this kind of testing. I used to use rspec and shoulda and do something like this in my model test:
it { should have_many(:products).through(:buying_guides_products) }
how would I go about testing something like this with minitest (preferably mintiest::spec if you're using that) Thanks for any help!
Don't test rails internals. Instead do something like
it "must have a products collection" do
@topic.must_respond_to :products
@topic.products.class.must_equal Array
@topic.products.size.must_be :>=, 1
@topic.products[0].class.must_equal Product
end
In these assertions you verify:
The @topic instance has access to the products collection
The collection is an array
The collection has elements
The collection's first element has a class of Product
Always test the expected result, and not the internal mechanisms. Test API and you will find that many of your testing issues vanish.
I assume this should work with javascript? Javascript isn't executed in my test runs, though it works fine in the browser. When my tests submits a form with :remote => true, I get the error"Unable to find xpath "/html"" and the page is blank when I call save_and_open_page. Any ideas? I tried :selenium and :webkit drivers.
As of the latest (07/07/2012) version of minitest-rails you don't need to create an Integration class and register it with MiniTest::Spec.
If you want to use Rails routing and/or Capybara matchers, you still need to include them. Like so:
ruby
ClassMiniTest::Rails::ActionDispatch::IntegrationTest
include Rails.application.routes.url_helpers
include Capybara::DSL
include Capybara::RSpecMatchersend
blowmage also has a minitest-rails-capybara that obviates the need to even do the above. However, I haven't been able to get it to work as of yet. It was only released today, so there's no point in being too picky :)
I'm also curious about the lack of minispec controller spec examples. Specifically, i've been looking for the "get", "post" and other http verb methods that Rspec supports, but I haven't seen that minispec supports these methods except via the minitest-rails gem. Has anyone seen any good examples of minispec controller specs?
Seems like the idea is not to test controllers directly. Controllers should become really thin after you move all app logic into your models. Then, you test your full stack (and indirectly your controllers) with integration tests.
It looks like in current rails (3.2.12 and sometime since this episode), application.rb now combines the require frameworks into rails/all. This make commenting out require 'rails/test_unit/railtie' problematic. So it runs the tests in default directories (the first test run: partial based on rails defaults).
Per this episode we are loading all the *_test.rb files which allows for the custom directories (the second test run: full).
There is probably a way to do the equivalent of commenting out the rails/test_unit/railtie require without having to be responsible to maintain the other requires that rails/all buys us... however you can get around this for now by adding in minitest.rake:
Does the final setup here use transactions in the db for isolation? If so, which part of the setup loads that support? I couldn't see it.
I had to use database_cleaner to clean up my testing database before each test:
This does not use transactions, although it should be possible somehow with database_cleaner. They have an RSpec example on their GitHub page which maybe could work.
This works for transactions (tons faster than
:truncation):What advantages does MiniTest have over other existing testing frameworks? Other than having "Mini" in the name.
It's small, fast and well designed. Ryan Davis gave an excellent talk on it, available here: http://confreaks.com/videos/618
Has anyone got capybara xpath assertions working?
Example:
page.should have_xpath("//title")
Gives me the error:
NoMethodError: undefined method `have_xpath'
have_xpathis an RSpec matcher.That's awesome. You figure out a lot of things I'm just lazy to dig out. I'm sure this ep will encourage people to use MiniTest in their projects.
Hm, actually when I create a new rails app it uses minitest automatically with ruby 1.9.
At least I see the seed option in my test output...
Yep, ruby1.9 implements test/unit using minitest.
So there is basically no need to setup all the extra classes, one can just use the rails internal stuff, including the test rake tasks...
@Ryan, is there a reason you created everything from scratch?
Or is it that when adding the minitest gem one has to do that to avoid using the ruby internal minitest?
Because you won't be able to use minitest's advanced features if you stick with test/unit.
Ah, okay, so I guess we can just wait for Rails 4 with fully integrated default minitest support and switch then ;-)
What minitest advanced features can't use if you don't use the minitest-rails gem?
Great! :-)
Ryan, do you already use MiniTest instead of the "classic" stack around RSpec ect as your standard approach? Or does it depends and differ from project to project?
Are there still some gaps, some gems of the testing-ecosystem (like guard, spork, vcr, jasmine ect) that do not work yet properly with MiniTest?
Or, asked different: for someone who isn't yet a pro in rails-testing, should I invest time in RSpec ect. or MiniTest?
Thanks again for this cool episode!
The mp4 and m4v videos for this episode don't seem to work on iOS 5.
Very nice episode !
I can't seem to get Simplecov working with Minitest.
The output is of 0% file coverage when i write on top of the minitest_helper.rb file :
Has anyone got this to work ?
Thanks a lot for your help.
Pretty awesome. I've been using RSpec for a while and the tests are really slow. I really like how light weight MiniTest is. Hopefully it'll run faster.
Has anyone used Minitest to test associations in their models? I can't seem to find any examples of this kind of testing. I used to use rspec and shoulda and do something like this in my model test:
it { should have_many(:products).through(:buying_guides_products) }
how would I go about testing something like this with minitest (preferably mintiest::spec if you're using that) Thanks for any help!
Don't test rails internals. Instead do something like
In these assertions you verify:
Always test the expected result, and not the internal mechanisms. Test API and you will find that many of your testing issues vanish.
BTW, Ryan, whats up with the end of the video. You started to answer the question of whether or not this will move you off of rspec.
I did the switch awhile back from rspec to riotrb, and got so used to fast tests, I dumped rspec altogether. Then I dumped riotrb for minispec.
Havent looked back
I am so confused. What is the difference between this and Cucumber?
I assume this should work with javascript? Javascript isn't executed in my test runs, though it works fine in the browser. When my tests submits a form with :remote => true, I get the error"Unable to find xpath "/html"" and the page is blank when I call save_and_open_page. Any ideas? I tried :selenium and :webkit drivers.
+1 wondering about this too
As of the latest (07/07/2012) version of minitest-rails you don't need to create an Integration class and register it with MiniTest::Spec.
If you want to use Rails routing and/or Capybara matchers, you still need to include them. Like so:
blowmage also has a minitest-rails-capybara that obviates the need to even do the above. However, I haven't been able to get it to work as of yet. It was only released today, so there's no point in being too picky :)
Just a quick follow up, minitest-rails-capybara is working now.
That works great - does this also have built in support for Helper tests?
What about testing controllers directly? Do you not do that? I am finding almost no examples of this, except for verifying which template is rendered.
I'm also curious about the lack of minispec controller spec examples. Specifically, i've been looking for the "get", "post" and other http verb methods that Rspec supports, but I haven't seen that minispec supports these methods except via the minitest-rails gem. Has anyone seen any good examples of minispec controller specs?
Seems like the idea is not to test controllers directly. Controllers should become really thin after you move all app logic into your models. Then, you test your full stack (and indirectly your controllers) with integration tests.
I suggest using minitest-spec-rails
https://github.com/metaskills/minitest-spec-rails
trying this with guard and capybara right now!
Great suggestion thanks!
Great screencast, thanks Ryan!
One problem I'm having (perhaps someone here has had similar?) is that my tests run twice, using the provided Rake task.
Basically, my model tests run, then my helper tests, and then ALL my tests (duplicating the aforementioned model and helper tests).
Anyone know what's up? I'm by no means a Rake guru...
It looks like in current rails (3.2.12 and sometime since this episode), application.rb now combines the require frameworks into rails/all. This make commenting out require 'rails/test_unit/railtie' problematic. So it runs the tests in default directories (the first test run: partial based on rails defaults).
Per this episode we are loading all the *_test.rb files which allows for the custom directories (the second test run: full).
There is probably a way to do the equivalent of commenting out the rails/test_unit/railtie require without having to be responsible to maintain the other requires that rails/all buys us... however you can get around this for now by adding in minitest.rake:
First sign in through GitHub to post a comment.