Archive

Posts Tagged ‘minitest’

Testing rubys initialize

October 6th, 2014 7 comments

In /ruby you have a lot of flexibility in writing code, this includes possibility to not initialize an object.
I use this for testing my initializers, here are some examples how to do it.

Lets start with ruby class:

class TestMe
  attr_reader :value
  def initialize(value = nil)
    @value = value
  end
end

It’s a simple example where only initialize can set a value, usually you use more advanced code, but this is just a minimal example.

Now lets test it:

describe TestMe do
  subject do
    TestMe.allocate
  end

  it "has default value" do
    subject.send :initialize
    subject.value.must_equal nil
  end

  it "sets value" do
    subject.send :initialize, 3
    subject.value.must_equal 3
  end

end

For more real live examples of allocate check