Testing Ruby + OOP +: How to check incoming command messages?

I just reread Sandy Mets's Practical Object Oriented Programming in Ruby, especially the testing chapter. Also a very useful conversation, which I recommend to rubists to watch: http://www.youtube.com/watch?v=URSWYvyc42M

She says to check out these cases:

  • Incoming requests: check them, claiming that they return.

  • Incoming team messages: check out direct public side effects (I have a question about this)

  • Requested messages are sent on their own: do not check them

  • Command messages sent by yourself: do not check them

  • Outgoing requests: do not check them

  • Outgoing command messages: check that they are sent

For No. 2, she gave an example similar to this:

#class
class Gear
  attr_reader :cog
  def set_cog(cog)
    @cog = cog
  end
end


# example spec
it "sets the value of @cog" do
  gear = Gear.new
  gear.set_cog(1)
  expect(gear.cog).to eq(1)
end

So, this is simple because it just sets the value of the instance variable, so the side effects are obvious. But what if my method calls another command message? For instance:

class Gear
    attr_reader :cog, :foo, :bar
    def set_cog(cog)
      reset_other_attributes
      @cog = cog
    end

  def reset_other_attributes
    @foo = nil
    @bar = nil
  end

end

How can I check this? I think that it should be considered as an outgoing command message, where you must state that this message has been sent, and conduct a separate test for the method reset_other_attributes.

it "calls the reset_other_attributes method" do
  gear = Gear.new
  gear.should_receive(:reset_other_attributes)
  gear.set_cog(1)
end

Is it correct?

+3
source share
2 answers

The real reason this method is difficult to verify is the fact that it violates the SRP principle. It sets more than the cog value.

, , , , reset_other_attributes. , , "reset_other_attributes" API.

0

.

, PROCESS, , A, B C , gobled, .

(, A , B , C ).

, . . , .

, . .

0

All Articles