36. require
'minitest/autorun'
class
Foo
def
add(x,
y)
x
+
y
end
end
describe
Foo
do
describe
'#add'
do
it
'adds
two
numbers'
do
Foo.new.add(4,
2).must_equal
6
end
end
end
Friday, October 4, 13
37. require
'minitest/autorun'
class
Foo
def
add(x,
y)
x
+
y
end
end
class
TestFoo
<
MiniTest::Unit::TestCase
def
test_add
assert_equal
6,
Foo.new.add(4,
2)
end
end
Friday, October 4, 13
47. describe
Something
do
it
"does
something"
do
#
tests
go
here
end
end
Friday, October 4, 13
48. describe
Something
do
before
do
#
setup
code
goes
here
end
after
do
#
tear
down
code
goes
here
end
it
"does
something"
do
#
tests
go
here
end
end
Friday, October 4, 13
49. describe
Something
do
describe
'something
else'
do
it
"does
something"
do
#
tests
go
here
end
end
end
Friday, October 4, 13
50. describe
Something
do
let(:something)
{
Something.new(name:
'Widget')
}
describe
'something
else'
do
it
"does
something"
do
something.name.must_equal
'Widget'
end
end
end
Friday, October 4, 13
51. describe
Something
do
subject
{
Something.new(name:
'Widget')
}
describe
'something
else'
do
it
"does
something"
do
subject.name.must_equal
'Widget'
end
end
end
Friday, October 4, 13
52. describe
Something
do
subject
{
Something.new(name:
'Widget')
}
context
'something
else'
do
it
"does
something"
do
subject.name.must_equal
'Widget'
end
end
end
Friday, October 4, 13
53. scrap.rb:19:in
`block
in
<main>':
undefined
method
`context'
for
#<Class:
0x007fce92074b40>
(NoMethodError)
from
/Users/markbates/.../lib/minitest/spec.rb:71:in
`class_eval'
from
/Users/markbates/.../lib/minitest/spec.rb:71:in
`describe'
from
scrap.rb:15:in
`<main>'
Friday, October 4, 13
59. describe
Something
do
it
'does
something'
it
'does
something
else'
do
skip
end
end
Friday, October 4, 13
60. describe
Something
do
it
'does
something'
it
'does
something
else'
do
skip
end
it
'does
another
thing'
do
skip
"here's
some
reason
why"
end
end
Friday, October 4, 13
65. describe
Something
do
it
"does
something"
do
lambda
{nil
/
2}.must_raise
NoMethodError
["one",
"two"].must_include
"one"
["one",
"two"].wont_include
"three"
nil.must_be_nil
[].must_be
:empty?
end
end
Friday, October 4, 13
68. class
TestSomething
<
MiniTest::Unit::TestCase
def
test_something
#tests
go
here
end
end
Friday, October 4, 13
69. class
TestSomething
<
MiniTest::Unit::TestCase
def
setup
#
setup
code
goes
here
end
def
teardown
#
tear
down
code
goes
here
end
def
test_something
#
tests
go
here
end
end
Friday, October 4, 13
70. class
TestSomething
<
MiniTest::Unit::TestCase
def
test_something
assert
true
end
end
Friday, October 4, 13
71. class
TestSomething
<
MiniTest::Unit::TestCase
def
test_something
assert
true
end
class
TestSomethingElse
<
MiniTest::Unit::TestCase
def
test_something_else
assert
true
end
end
end
Friday, October 4, 13
76. class
SomethingTest
<
MiniTest::Unit::TestCase
def
test_something
skip
end
end
Friday, October 4, 13
77. class
SomethingTest
<
MiniTest::Unit::TestCase
def
test_something
skip
end
def
test_something_else
skip
"here's
some
reason
why"
end
end
Friday, October 4, 13
78. #
Running
tests:
SS
Finished
tests
in
0.000663s,
3016.5913
tests/s,
0.0000
assertions/s.
2
tests,
0
assertions,
0
failures,
0
errors,
2
skips
Friday, October 4, 13
84. describe
Something
do
let(:something)
{
MiniTest::Mock.new
}
it
"does
something"
do
end
end
Friday, October 4, 13
85. describe
Something
do
let(:something)
{
MiniTest::Mock.new
}
it
"does
something"
do
something.expect(:name,
'Widget')
end
end
Friday, October 4, 13
86. describe
Something
do
let(:something)
{
MiniTest::Mock.new
}
it
"does
something"
do
something.expect(:name,
'Widget')
something.name.must_equal
'Widget'
end
end
Friday, October 4, 13
87. describe
Something
do
let(:something)
{
MiniTest::Mock.new
}
it
"does
something"
do
something.expect(:name,
'Widget')
something.name.must_equal
'Widget'
something.verify
end
end
Friday, October 4, 13
88. describe
Something
do
let(:something)
{
MiniTest::Mock.new
}
it
"does
something"
do
something.expect(:name,
'Widget')
something.verify
end
end
Friday, October 4, 13
90. describe
Something
do
let(:something)
{
Something.new(name:
'Widget')
}
it
"does
something"
do
end
end
Friday, October 4, 13
91. describe
Something
do
let(:something)
{
Something.new(name:
'Widget')
}
it
"does
something"
do
something.stub(:name,
'Thingy')
do
end
end
end
Friday, October 4, 13
92. describe
Something
do
let(:something)
{
Something.new(name:
'Widget')
}
it
"does
something"
do
something.stub(:name,
'Thingy')
do
something.name.must_equal
'Thingy'
end
end
end
Friday, October 4, 13
94. describe
"Something"
do
let(:something)
{
OpenStruct.new(name:
'Widget')
}
it
"does
something"
do
something.name.must_equal
'Widget'
end
end
Friday, October 4, 13
95. describe
Something
do
let(:something)
{
Something.new(name:
'Widget')
}
it
"does
something"
do
something.name.must_equal
'Thingy'
end
end
Friday, October 4, 13
96. describe
Something
do
let(:something)
{
Something.new(name:
'Widget')
}
it
"does
something"
do
def
something.name
"Thingy"
end
something.name.must_equal
'Thingy'
end
end
Friday, October 4, 13
102. module
MiniTest::Assertions
def
assert_equals_rounded(rounded,
decimal)
assert
rounded
==
decimal.round,
"Expected
#{decimal}
to
round
to
#{rounded}"
end
end
Friday, October 4, 13
103. module
MiniTest::Assertions
def
assert_equals_rounded(rounded,
decimal)
assert
rounded
==
decimal.round,
"Expected
#{decimal}
to
round
to
#{rounded}"
end
def
refute_equals_rounded(rounded,
decimal)
end
end
Friday, October 4, 13
104. module
MiniTest::Assertions
def
assert_equals_rounded(rounded,
decimal)
assert
rounded
==
decimal.round,
"Expected
#{decimal}
to
round
to
#{rounded}"
end
def
refute_equals_rounded(rounded,
decimal)
assert
rounded
!=
decimal.round,
"Expected
#{decimal}
to
not
round
to
#{rounded}"
end
end
Friday, October 4, 13
105. module
MiniTest::Assertions
def
assert_equals_rounded(rounded,
decimal)
assert
rounded
==
decimal.round,
"Expected
#{decimal}
to
round
to
#{rounded}"
end
def
refute_equals_rounded(rounded,
decimal)
assert
rounded
!=
decimal.round,
"Expected
#{decimal}
to
not
round
to
#{rounded}"
end
end
Numeric.infect_an_assertion
:assert_equals_rounded,
:must_round_to
Numeric.infect_an_assertion
:refute_equals_rounded,
:wont_round_to
Friday, October 4, 13
106. describe
Something
do
it
"does
something"
do
1.2.must_round_to
1
1.7.wont_round_to
1
end
end
Friday, October 4, 13
109. ENV["RAILS_ENV"]
=
"test"
require
File.expand_path("../../config/environment",
__FILE__)
require
"rails/test_help"
require
"minitest/rails"
#
Add
`gem
"minitest-‐rails-‐capybara"`
to
the
test
group
of
your
Gemfile
#
and
uncomment
the
following
if
you
want
Capybara
feature
tests
#
require
"minitest/rails/capybara"
Friday, October 4, 13
110. ENV["RAILS_ENV"]
=
"test"
require
File.expand_path("../../config/environment",
__FILE__)
require
"rails/test_help"
require
"minitest/rails"
#
Add
`gem
"minitest-‐rails-‐capybara"`
to
the
test
group
of
your
Gemfile
#
and
uncomment
the
following
if
you
want
Capybara
feature
tests
#
require
"minitest/rails/capybara"
class
ActiveSupport::TestCase
#
Add
more
helper
methods
to
be
used
by
all
tests
here...
before
do
end
after
do
end
end
Friday, October 4, 13
111. ENV["RAILS_ENV"]
=
"test"
require
File.expand_path("../../config/environment",
__FILE__)
require
"rails/test_help"
require
"minitest/rails"
#
Add
`gem
"minitest-‐rails-‐capybara"`
to
the
test
group
of
your
Gemfile
#
and
uncomment
the
following
if
you
want
Capybara
feature
tests
#
require
"minitest/rails/capybara"
module
MiniTest::Expectations
infect_an_assertion
:assert_redirected_to,
:must_redirect_to
infect_an_assertion
:assert_template,
:must_render_template
infect_an_assertion
:assert_response,
:must_respond_with
end
class
ActiveSupport::TestCase
#
Add
more
helper
methods
to
be
used
by
all
tests
here...
before
do
end
after
do
end
end
Friday, October 4, 13
112. rake
minitest
#
Run
default
tests
rake
minitest:all
#
Run
all
tests
rake
minitest:all:quick
#
Run
all
tests,
ungrouped
for
quicker
execution
rake
minitest:controllers
#
Runs
tests
under
test/controllers
rake
minitest:lib
#
Runs
tests
under
test/lib
rake
minitest:models
#
Runs
tests
under
test/models
Friday, October 4, 13
114. ruby
-‐I
test
test/models/user_test.rb
Friday, October 4, 13
115. describe
FeedController
do
describe
'#index'
do
it
'renders
an
atom
feed'
do
get
:index
must_render_template
:index
end
it
'redirects
from
an
RSS
feed'
do
get
:index,
format:
'rss'
must_redirect_to
feed_path(format:
:atom)
end
end
end
Friday, October 4, 13