Rails Minitest Style Guide

In the test,  should use ‘describe’,  ‘context’, ‘it’ structure.

Here I use a controller test as an example.

I have this my_controller.rb with two controller actions:

class MyController < ApplicationController
  def controller_action_1
  # Code goes here
  end

  def controller_action_2
  # Code goes here
  end
end

And controller test file my_controller_test.rb:

require ‘test_helper’

class MyControllerTest < ActionController::TestCase
# Initialize the variables that will be used in all describe block tests within MyControllerTest class
before do
v_block = ‘this variable is only accessible in this block’
@v_describe_level = ‘this varible is accessible in all describe blocks’
end

# Test for controller action 1
describe ‘#controller_action_1’ do
# Intialize the variables that will be used in all context block tests within this describe block
before do
v_block = ‘this variable is only accessible in this block’
@v_context_level = ‘this varible is accessible in all context blocks’
end

context ‘when the user is admin’ do
# Intialize the variables that will be used in all it block tests within this context block
before do
v_block = ‘this variable is only accessible in this block’
@v_it_level = ‘this varible is accessible in all it blocks’
end

it ‘must has admin role’ do
# Code goes here. Examples: http://ruby-doc.org/stdlib-1.9.3/libdoc/minitest/spec/rdoc/MiniTest/Expectations.html
# must_be
# must_be_empty
# must_be_nil
# must_be_same_as
# must_equal
# must_raise
# wont_be
# wont_be_nil
# wont_equal
# wont_include
end

it ‘must redirect to admin management page’ do
# Code goes here, example:
# assert_redirected_to
end
end

context ‘when the user is not admin’ do
it ‘must not have admin role’ do
end

it ‘must redirect to non-admin management page’ do
end
end
end

describe ‘#controller_action_2’ do
# Code goes here
end

private

# Helper functions goes here
end

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s