nebulab / simple_command

A simple, standardized way to build and use Service Objects (aka Commands) in Ruby
http://nebulab.it
MIT License
624 stars 55 forks source link

how to test with rspec? #6

Closed thg303 closed 7 years ago

thg303 commented 7 years ago

I have written a couple of commands using this amazing gem, I wonder what is the best approach to test them with rspec.

kennyadsl commented 7 years ago

@thg303 Hi there, thanks for using this gem.

Suppose to have a command like:

class MyCommand
  prepend SimpleCommand

  def initialize(something)
    @something = something
  end

  def call
    if @something == :ok
      return
    else
      errors.add(:something, 'Something Wrong')
    end
    nil
  end
end

You can create a spec similar to this one:

describe MyCommand do
  subject(:context) { described_class.call(something) }

  describe '.call' do
    context 'when the context is successful' do
      let(:something) { :ok }

      it 'succeeds' do
        expect(context).to be_success
      end
    end

    context 'when the context is not successful' do
      let(:something) { :wrong }

      it 'fails' do
        expect(context).to be_failure
      end
    end
  end
end

If this works and you want to contribute, feel free to open a PR with something about using it with RSpec in the README, thanks!

thg303 commented 7 years ago

it worked. thanks. I submitted a PR for this