#327 MiniTest with Rails pro
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.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
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
):Another great tutorial by Ryan!
MiniTest has evolved significantly since this tutorial was created. There are changes in configuration, and to help clarify the differences between the 3 main test frameworks I wrote a guide that examines their specific characteristics.
Here is a link to Ruby Reflections where you can find configuration for Rails 4.1 Test::Unit, MiniTest, Rspec, and combined with fixtures or factories. Pattern Objects, mocking, stubbing, and debugging hints are also covered.
Database cleaner is also explained, when it is necessary and when it is not.
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_xpath
is 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?
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?
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:
I'm having a blast with MiniTest, thanks for the cast to get me started.
BTW, does anyone know what the heck is going on here? I cant get rid of these INFO logs when running rake test:
The INFO log shows up for every test.
I followed through the debugger and discovered it is not a MiniTest issue. The message is coming from ActiveSupport::Testing::TaggedLogging.
All fixed!
One of my favorites Railscasts!
Bestforyourhome is India’s leading website in providing in-depth reviews of best home and kitchen appliances for your home. If you are planning to buy your next vacuum cleaner, induction cooktop, water purifier, coffee maker or any other electronic device for your home or kitchen, you’re at the perfect place.
Welcome to BestForYourHome.co.in
Bestforyourhome.co.in is India’s leading website in providing in-depth reviews of best home and kitchen appliances for your home. If you are planning to buy your next vacuum cleaner, induction cooktop, water purifier, coffee maker or any other electronic device for your home or kitchen, you’re at the perfect place.