Rails Conf 2013: The Magic Tricks of Testing by Sandi Metz

I’ve watched a great video on Youtube about Testing, by the awesome Sandi Metz. I really like her style when shes holding speeches at Ruby Cons. I think the whole Testing thing is really important for every developer so I give it a try and summarize what she said.

Testing

Alot of programmers dont bother covering theire code with tests, they simply hate their tests because its slow and interrupts their productivity. But there are ways to ease the way about integrating tests.

Unit Tests

We want our object to be thoroghly working correctly. We  want out tests to be stable and fast. The object under tests receive messages, send messages out and to themselves, its important to focus on those three origins.

message types:

  • can be queries or commands

Queries:

a calculation is a Query e.g. , has no sideeffect and just care about whats incoming

Commands:

they have sideeffects but you dont care about whats incoming, quite the opposite as Queries.

The reason to keep an eye on both message types is that they are tested differently.

Incoming Query Messages:

making assertions about what they send back

Example:

class Wheel

attr_reader :rim, :tire

   def initialize(rim,tire)

   end

def diameter   #is a query that just returns stuff, but doesnt change anything

rim + ( tire * 2 )

   end

end

Testing code: (Mini Test)

class WheelTest < MiniTest::Unit::TestCase

def test_calculates_diameter

wheel = wheel.new(26, 1,5)

assert_in_delta(29,wheel.diameter, 0,01)  #assertion about the value when we send the                                                                                 message

end

end

Incoming Command Messages:

making assertions about direct public side effects

class Gear

attr_reader :chainring, :cog, :wheel

 def initialize(args)

#…

     end

def set_cog(new_cog)

@cog = cog.new   #Query/Command combo, returns the result

      end

end

Testing code:

class GearTest < MiniTest::Unit::TestCase

 def test_set_cog

gear = Gear.new   #giving it an instance of gear

gear.set.cog(27)   #sends the message that causes the sideeffect

assert(27, gear.cog)   #making the assertion about the value of the sideeffect

    end

end

So both Incoming Querys and Commands are about testing values.

Messages Sent to Self

Dont test private methods. Do not make assertions about theire result. Do not expect to send them.

=> Ignore both Queries and Commands send to self!

Outgoing Query Messages

Dont test outgoing Query messages. Do not make assertions about theire results. Do not expect to send them!

Outgoing Command Messages

Expect to send outgoing command messages

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