z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
r | reload slides |
n | toggle notes |
p | run preshow |
(in Ruby)
(in Ruby on Rails)
(in Ruby on Rails for .NET devs)
test "the truth" do
assert true
end
test "the truth" do
assert false
end
test "the truth" do
assert false
end
==
Failure:
test: the truth [dir/file_test:123]:
<false> is not true.
http://guides.rubyonrails.org/testing.html#assertions-available
assert( boolean, [msg] )
Ensures that the object/expression is true.
assert_equal( obj1, obj2, [msg] )
Ensures that obj1 == obj2 is true.
assert_not_equal( obj1, obj2, [msg] )
Ensures that obj1 == obj2 is false.
assert_same( obj1, obj2, [msg] )
Ensures that obj1.equal?(obj2) is true.
assert_not_same( obj1, obj2, [msg] )
Ensures that obj1.equal?(obj2) is false.
assert_nil( obj, [msg] )
Ensures that obj.nil? is true.
assert_not_nil( obj, [msg] )
Ensures that obj.nil? is false.
assert_match( regexp, string, [msg] )
Ensures that a string matches the regular expression.
assert_no_match( regexp, string, [msg] )
Ensures that a string doesn’t match the regular expression.
assert_in_delta( expecting, actual, delta, [msg] )
Ensures that the numbers expecting and actual are within delta of each other.
assert_throws( symbol, [msg] ) {
block
}
Ensures that the given block throws the symbol.
assert_raise( exception1, ... ) {
block
}
Ensures that the given block raises one of the given exceptions.
assert_nothing_raised( exception1, ... ) {
block
}
Ensures that the given block doesn’t raise one of the given exceptions.
assert_instance_of( class, obj, [msg] )
Ensures that obj is of the class type.
assert_kind_of( class, obj, [msg] )
Ensures that obj is or descends from class.
assert_respond_to( obj, symbol, [msg] )
Ensures that obj has a method called symbol.
assert_operator( obj1, operator, obj2, [msg] )
Ensures that obj1.operator(obj2) is true.
assert_send( array, [msg] )
Ensures that executing the method listed in array[1] on the object in array[0] with the parameters of array[2 and up] is true. This one is weird eh?
flunk( [msg] )
Ensures failure. This is useful to explicitly mark a test that isn’t finished yet.
def first_name(name)
name.split(" ").first
end
def first_name(name)
name.split(" ").first
end
test "first name" do
assert_equal "John", first_name("John Doe")
end
def first_name(name)
name.split(" ").first
end
test "first name" do
assert_equal "John", first_name("John Doe")
assert_instance_of String, first_name("John Doe")
end
def first_name(name)
name.split(" ").first
end
test "return first name" do
assert_equal "John", first_name("John Doe")
end
test "return a string" do
assert_instance_of String, first_name("John Doe")
end
def first_name(name)
name.split(" ").first
end
test "return first name" do
assert_equal "Jane", first_name("Jane Doe")
assert_equal "John", first_name("John Doe")
assert_equal "Nice", first_name("Mr. Nice Guy")
end
test "return a string" do
assert_instance_of String, first_name("John Doe")
end
Failure:
test: return first name [dir/file_test:123]:
<"Nice"> expected but was <"Mr.">
test "strip Mr. from the name" do
assert_equal "John", first_name("Mr. John Doe")
end
def first_name(name)
name.gsub! /^Mr\.\s/, ''
name.split(" ").first
end
test "strip Mr. from the name" do
assert_equal "John", first_name("Mr. John Doe")
end
test "strip Mrs. from the name" do
assert_equal "Jane", first_name("Mrs. Jane Doe")
end
def first_name(name)
name.gsub! /^Mr[s\.]*\s/, ''
name.split(" ").first
end
test "strip the honorific from the name" do
assert_equal "John", first_name("Mr. John Doe")
assert_equal "Jane", first_name("Mrs. Jane Doe")
end
test "return first name" do
assert_equal "John", first_name("John Doe")
end
test "strip the honorific from the name" do
assert_equal "John", first_name("Mr. John Doe")
assert_equal "Jane", first_name("Mrs. Jane Doe")
end
test "return a string" do
assert_instance_of String, first_name("John Doe")
end
describe "object" do
test "do something" do
assert something
end
test "do something else" do
assert something(else)
end
end
context "object" do
should "do something" do
assert something
end
should "do something else" do
assert something(else)
end
end
# user_test.rb
class UserTest < Test::Unit::TestCase
context "age" do
should "return a fixnum" do
assert_instance_of Fixnum,
User.first.age
end
end
end
Failure:
test: age should return a fixnum.
(UserTest) [user_test.rb:4]
Expected "age" to be an instance of
Fixnum, not String.
# user_test.rb
class UserTest < Test::Unit::TestCase
context "age" do
setup do
@user = User.first
end
should "return a fixnum" do
assert_instance_of Fixnum, @user
end
end
end
context "first name" do
setup do
@john = first_name "John Doe"
@mr_john = first_name "Mr. John Doe"
@mrs_jane = first_name "Mrs. Jane Doe"
end
should "return the first name" do
assert_equal "John", @john
end
should "strip the honorific from the name" do
assert_equal "John", @mr_john
assert_equal "Jane", @mrs_jane
end
should "return a string" do
assert_instance_of String, @john
end
end
(for websites)
test "should get index" do
get "/index"
assert_response 200
end
test "should get index" do
get "/index"
assert_response 200
assert_select "h1", "OurCompany"
end
test "should post contact form" do
post "/contact", :post => {
:email => "john@doe.com",
:message => "I really like your site"
}
assert_response 201
end
test "should validate email" do
post "/contact", :post => {
:email => "jo@hn@do@e.com",
:message => "I really like your site"
}
assert_response 406
assert_select ".error", "Invalid email"
end
test "login and browse site" do
get "/login"
assert_response :success
post_via_redirect "/login",
:username => "joe",
:password => "secret"
assert_equal '/welcome', path
assert_select "#username", "Welcome Joe!"
get "/secret/stuff"
assert_response :success
end
github.com/Movitas/schlep-ruby
def event(type, message)
envelope = {
:timestamp => Time.now.to_f,
:type => type,
:message => message.to_json
}.to_json
@redis.rpush 'schlep', envelope
end
context "timestamp" do
should "be a float" do
# ???
end
end
def timestamp
Time.now.to_f
end
def event(type, message)
envelope = {
:timestamp => timestamp,
:type => type,
:message => message.to_json
}.to_json
@redis.rpush 'schlep', envelope
end
context "timestamp" do
should "be a float" do
assert_instance_of Float,
Schlep.timestamp
end
end
context "???" do
should "be valid json" do
# ???
end
end
context "envelope" do
should "be valid json" do
assert_nothing_raised(Exception) {
JSON.parse(
Schlep.envelope "test_type",
{ :one => { :two => 3 }}
)
}
end
end
def envelope(type, message)
{
:timestamp => timestamp,
:type => type,
:message => message.to_json
}.to_json
end
def event(type, message)
@redis.rpush 'schlep',
envelope(type, message)
end
:message => message.to_json
context "serialize message" do
should "leave valid json alone" do
assert_equal "{\"one\":{\"two\":3}}",
Schlep.serialize_message("{\"one\":{\"two\":3}}")
end
end
def serialize_message(message)
return message if message.is_a? String
message.to_json
end
def envelope(type, message)
{
:timestamp => timestamp,
:type => type,
:message => serialize_message(message)
}.to_json
end
context "serialize message" do
should "leave valid json alone" do
assert_equal "{\"one\":{\"two\":3}}",
Schlep.serialize_message("{\"one\":{\"two\":3}}")
end
should "leave strings alone" do
assert_equal "test string",
Schlep.serialize_message("test string")
end
end
def event(type, message)
# Creates an envelope
# Parses the message to JSON
# Parses the envelope to JSON
# Sends the event to Redis
end
def envelope(type, message)
# Creates an envelope and parses to JSON
end
def event(type, message)
# Sends an event
end
def serialize_message(message)
# Parses a message to valid JSON
end
def timestamp
# Returns a timestamp in the format we want
end
(CI)