How can I state that when initializing behavior with RSpec?

I have a message class that can be initialized by passing arguments to the constructor or passing any arguments, and then setting the attributes later using accessories. Attribute setting methods have some preprocessing.

I have tests that ensure that the setter methods do what they assume, but I cannot find a good way to verify that the initialization method actually calls the setters.

class Message
  attr_accessor :body
  attr_accessor :recipients
  attr_accessor :options

  def initialize(message=nil, recipients=nil, options=nil)
    self.body = message if message
    self.recipients = recipients if recipients
    self.options = options if options
  end

  def body=(body)
    @body = body.strip_html
  end
  def recipients=(recipients)
    @recipients = []
    [*recipients].each do |recipient|
      self.add_recipient(recipient)
    end
  end
end
+5
source share
1 answer

I would try to check the behavior of the initializer,

i.e. that its setting variables as you expect.

, , , , , , , . unit test.

.

describe "initialize" do
  let(:body) { "some text" }
  let(:people) { ["Mr Bob","Mr Man"] }
  let(:my_options) { { :opts => "are here" } }

  subject { Message.new body, people, my_options }

  its(:message)    { should == body }
  its(:recipients) { should == people }
  its(:options)    { should == my_options }
end
+4

All Articles