how to test with rspec?
thg303 opened this issue · comments
Ali Ghanavatian commented
I have written a couple of commands using this amazing gem, I wonder what is the best approach to test them with rspec.
Alberto Vena commented
@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!
Ali Ghanavatian commented
it worked. thanks. I submitted a PR for this