I'm building an app including a Rails API and want to use Ruby MiniTest::Spec to test.
What's a good way to set it up?
For example, good directory organization, good way to include files, etc.?
I'm using the guidelines in the book Rails 3 In Action which uses RSpec and has a great chapter on APIs. The big change is preferring MiniTest::Spec.
What is Minitest? Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.
To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.
Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case. You can also run a particular test method from the test case by providing the -n or --name flag and the test's method name.
Answering with what I've found so far in case it's helpful to other developers....
require 'spec_helper'
class ItemsSpec < ActionDispatch::IntegrationTest
before do
@item = Factory.create(:item)
end
describe "items that are viewable by this user" do
it "responds with good json" do
get "/api/items.json"
response.success?.must_equal true
body.must_equal Item.all.to_json
items = JSON.parse(response.body)
items.any?{|x| x["name"] == @item.name}.must_equal true
end
end
end
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
gem 'minitest'
require 'minitest/autorun'
require 'action_controller/test_case'
require 'capybara/rails'
require 'rails/test_help'
require 'miniskirt'
require 'factories'
require 'mocha'
# Support files
Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each do |file|
require file
end
Factory.define :item do |x| x.name { "Foo" } end
class Api::BaseController < ActionController::Base
respond_to :json
end
class Api::ItemsController < Api::BaseController
def index
respond_with(Item.all)
end
end
MyApp::Application.routes.draw do
namespace :api do
resources :items
end
end
group :development, :test do gem 'capybara' # Integration test tool to simulate a user on a website. gem 'capybara_minitest_spec' # MiniTest::Spec expectations for Capybara node matchers. gem 'mocha' # Mocking and stubbing library for test doubles for Ruby. gem 'minitest', '>= 3' # Ruby's core TDD, BDD, mocking, and benchmarking. gem 'minitest-capybara' # Add Capybara driver switching parameters to minitest/spec. gem 'minitest-matchers' # RSpec/Shoulda-style matchers for minitest. gem 'minitest-metadata' # Annotate tests with metadata key-value pairs. gem 'minitest-spec-rails' # Drop in MiniTest::Spec support for Rails 3. gem 'miniskirt' # Factory creators to go with minitest. gem 'ruby-prof' # Fast code profiler for Ruby with native C code. end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With