Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

Mini-Test

Treehouse

Some examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'minitest/autorun'

class MyTest < MiniTest::Unit::TestCase
#all tests are prefixed with "test_"
  def test_that_addition_works
      assert_equal 4, 2+2
  end

  def test_that_my_arry_has_a_value
      my_array = %w(dog cat frog)
      assert my_array.include?('frog')
  end

#asser_delta: checks to see that floating point numbers are within a given range
  def test_assert_delta
      assert_in_delta 3.14159, 3, 0.2
  end

#assert_match: checks to see if the reg expressions evaluates against a string
  def test_assert_match
      assert_match /world/, 'Hello world'
  end

#assert_nil: all variables passed in are nil
  def test_nil
      some_variables = nil
      assert_nil some_variable
  end

#assert_output
  def test_output
      assert_output "hello world", nil do
          print 'hello world'
      end
  end

#assert_raises
  def test_raise
      assert_raises NameError do
          this_variable_doesnt_exist
      end
  end

#assert_respond_to: testing if something responds to a certain method
  def test_respond
      assert_respond_to Array.new, 'include?'
  end
  
end

All tests are prefixed with “test_”

Mini Test Spec

The functionality of the Mini-test and Mini-test Spec are pretty similar. The keywords and strucutre are different as shown below…

Mini Test Spec Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'minitest/autorun'
require 'minitest/spec'

describe 'MyTests' do

  #can add before and after blocks
  before do
      puts "Tests are beginning."
  end

  it "adds 2 + 2" do
      (2+2).must_equal 4
  end

  it "includes a frog" do
      %w(dog cat frog).must_include('frog')
  end

  it "must be Fixnum" do
      (2+2).must_be_instance_of Fixnum
  end

  it "should raise an error" do
      array = []
      lambda { arrary.hello }.must_raise NoMethodError
  end

Test Driven Development

Write just enough code to make the test pass

Comments