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.
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.
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.
- can be queries or commands
a calculation is a Query e.g. , has no sideeffect and just care about whats incoming
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
attr_reader :rim, :tire
def diameter #is a query that just returns stuff, but doesnt change anything
rim + ( tire * 2 )
Testing code: (Mini Test)
class WheelTest < MiniTest::Unit::TestCase
wheel = wheel.new(26, 1,5)
assert_in_delta(29,wheel.diameter, 0,01) #assertion about the value when we send the message
Incoming Command Messages:
making assertions about direct public side effects
attr_reader :chainring, :cog, :wheel
@cog = cog.new #Query/Command combo, returns the result
class GearTest < MiniTest::Unit::TestCase
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
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