Rails 4 Minitest minimal setup - ruby-on-rails

I notice after reading the rails guide docs
that minitest comes with the newer Ruby versions out of the box, so it's available right away, no need to install anything else. Rails 4 by default use that.
If fact i use the minitest-reporter gem and it work like charm.
The confusion comes when i see examples defining the :test group of the gemfile like this:
group :test do
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'minitest-colorize'
gem 'minitest-focus'
end
Why is the need of include a minitest-rails gem ?? To use some rails generation later or other kind of specs ??
I'm kinda confused here so any thought about Rails 4 Minitest integration will be appreciated.

According to the mini-test rails README, it makes it so Rails generators for tests will generate in minitest style. It also makes it so minitest style actually works in Rails ActiveSupport::TestCase test classes. (which is what Rails test cases do).
minitest comes with ruby, but the Rails' ActiveSupport::TestCase, which Rails test classes all are, doesn't normally use minitest. minitest-rails makes it so it does.
This project aims to enable Minitest within the Rails TestCase classes. Your test will continue to inherit from ActiveSupport::TestCase, which will now support the spec DSL. You can generate test files with the standard model, controller, resource, and other generators:
https://github.com/blowmage/minitest-rails
One thing that makes this more confusing, is that in newer versions of ruby [i accidentally said 'Rails' before, but it's newer versions of ruby that matter], it's true that Minitest is there and old Test::Unit is not. But Minitest has a sort of "pretend to be Test::Unit" mode -- that's what Rails ActiveSupport::TestCase ordinarily uses, the old Test::Unit style of testing -- even if it's actually the minitest library providing it. minitest-rails makes it support the newer Minitest::Spec::DSL style.

minitest-rails is already a dependency of minitest-rails-capybara which you are using and have added to the test group.
So regardless of whether you explicitly add minitest-rails to the Gemfile or not, it will be installed for your application. However, you might wish to explicitly specify a specific version of minitest-rails that you want to use, and in such case you should add the gem to your Gemfile with the wanted version (something you are not doing in your code).

If you have no interest in using Minitest's spec DSL then you don't need to include minitest-rails. If you have no interest in Capybara's spec DSL then you don't need to include minitest-rails-capybara, although if you want to use Capybara you will need some other mechanism to add it to your application. The minitest-capybara gem doesn't do any rails integration.
It is possible that these two libraries are added to add the Capybara support even though you use test style tests and not spec style tests. The minitest-rails-capybara gem adds generators for Capybara tests in both test style and spec style. Try git blame on your Gemfile to find the commit where those two lines were added, as the commit message might explain why they are in the application.

Related

Rspec testing of Rails gem

At my job we have a number of rails projects with similar functionality. We have refactored some of the common functionality out into a gem that is included in all the projects.
The gem is just another rails project, providing models, controllers, and views to be added to the projects that include it.
How do I test the gem? It's not clear to me how to test it independently, but it doesn't make sense to write tests of the gem in the projects that include it.
You should have tests in the gem's project itself.
As an example you can see that cancancan has a directory structure similar to all other rails projects, with a root level 'spec' folder.
You may have to use rspec rather than rspec-rails depending on your gem setup, but there isn't any real magic sauce here. You should write tests that cover the functionality of the gem itself, within the gem's codebase. But you shouldn't be writing tests for the gem in the projects that include it. Or at least not more than you might for other gems (so there may be some mocking / verification / interface building, but no direct testing of the gem).

What's the difference b/w minitest-rails and minitest-spec-rails?

Looking into using Minitest for an existing Rails 3.2.
I'm trying to understand the difference between minitest-rails and minitest-spec-rails.
The main differences between minitest-spec-rails and minitest-rails is the scope of the problems they solve.
minitest-spec-rails
minitest-spec-rails overrides the test infrastructure that Rails provides by default and adds the ability to use the Minitest Spec DSL and matchers in your tests. This means that you don't have to make any changes to your existing tests to start using the Spec DSL. It also means that the Spec DSL is available in every test.
minitest-rails
minitest-rails adds the ability to use the Spec DSL, but also changes the approach to how tests are written. It provides new generators for your tests, and allows you to choose from the TDD-style assertions or the BDD-style expectations. It places test files in more sensible locations.
It also allows your existing tests to live side by side with your new Minitest tests. You have the option to override the default test infrastructure similar to minitest-spec-rails, but you also have the option to leave them untouched.
Disclosure: I am the author of minitest-rails
With minitest-spec-rails, we have a working solution by replacing MiniTest::Spec as the superclass for ActiveSupport::TestCase. This solution is simple and does not require you to recreate a new test case in your test_helper.rb or to use generators supplied by gems like minitest-rails.
Minitest changes for testing within Rails. Your test classes will inherit from MiniTest::Rails::ActiveSupport::TestCase a opposed to ActiveSupport::TestCase. You can use the MiniTest::Spec DSL. You can generate test files with the standard model, controller, resource, and other generators.
rails generate model User
or
And you can specify generating the tests using the MiniTest::Spec DSL on any of the generators by providing the --spec option
rails generate model User --spec

How to load fixtures using Minitest and Rails 3.0.10?

I have a Rails 3.0.10 project using the minitest-rails gem (running in JRuby 1.6.5 == Ruby 1.9.2). I would like to use all of our existing Test::Unit test cases with minimal changes in order to start using Minitest. Right now I can run tests but many fail because fixtures are not being loaded. I have yet to figure out how to make that work. If I change ActionController::TestCase to MiniTest::Unit::TestCase then the fixtures function doesn't work. I've tried the links here and the links within that one without any success. So the question is how to continue using ActionController::TestCase with the MiniTest gem or how to switch to MiniTest::Unit::TestCase and have fixtures load.
Please try upgrading to version 0.1, which is dramatically improved across the board. Stuff like fixtures actually work now.

RSpec/Gem development: Stubbing rails templates

I'm currently working on a couple of different gems both of which mainly consist of rails view helpers.
My problem is that some of these helpers require rendered rails templates to test the output - but I am unsure of the best way to stub a template, without a rails application. I presume that I should be able to leverage rspec-rails in some capacity, but i've been having trouble getting that to work without a rails app present.
Am I approaching this the wrong way? What's the current best-practice to test rails-specific features (in particular - things that happen only in the view) during gem development?
I use the excellent enginex gem, which helps you in setting up a gem skeleton with an embedded rails application for testing. This way you can easily test any rails dependency inside that application. To generate rspec tests run it as follows (default is test-unit):
enginex -t rspec your-gem-name
What I did to incorporate this into my gem, was run this inside some test folder, and copied the necessary files over to my gem. As an example, you could check it out inside my cocoon gem.
Hope this helps.

Ruby Gem or Rails plugin

I am writing a gem that requires rails as a dependency (for using Rails methods like underscore, camelize and others).
Is 'gem' still a gem or is it now something else (Rails Plugin?) since I have rails as a dependency?
You don't have to depend on Rails as a whole (in fact if it really is a standalone gem, I would recommend that you do not) you can depend on just the parts of it that provide the functionality you need, in this case you are talking about ActiveSupport.

Resources