CMDx::RSpec
Collection of RSpec matchers for CMDx.
Installation
Add this line to your application's Gemfile:
gem 'cmdx-rspec'And then execute:
$ bundle
Or install it yourself as:
$ gem install cmdx-rspec
Matchers
be_successful
Asserts that a CMDx task result indicates successful execution.
it "returns success" do
result = SomeTask.execute
expect(result).to be_successful
endhave_skipped
Asserts that a CMDx task result indicates the task was skipped during execution.
it "returns skipped" do
result = SomeTask.execute
# Default result
expect(result).to have_skipped
# Custom result
expect(result).to have_skipped(
reason: "Skipping for a custom reason",
cause: be_a(CMDx::SkipFault)
# Other members of `result.to_h`...
)
endhave_failed
Asserts that a CMDx task result indicates execution failure.
it "returns failure" do
result = SomeTask.execute
# Default result
expect(result).to have_failed
# Custom result
expect(result).to have_failed(
reason: "Failed for a custom reason",
cause: be_a(NoMethodError)
# Other members of `result.to_h`...
)
endhave_empty_metadata
Asserts that a CMDx task result has no metadata.
it "returns empty metadata" do
result = SomeTask.execute
expect(result).to have_empty_metadata
endhave_matching_metadata
Asserts that a CMDx task result contains specific metadata.
it "returns matching metadata" do
result = SomeTask.execute
expect(result).to have_matching_metadata(status_code: 500)
endhave_empty_context
Asserts that a CMDx task result has no context data.
it "returns empty context" do
result = SomeTask.execute
expect(result).to have_empty_context
endhave_matching_context
Asserts that a CMDx task result contains specific context data.
it "returns matching context" do
result = SomeTask.execute
expect(result).to have_matching_context(stored_result: 123)
endbe_deprecated
Asserts that a CMDx task result indicates the task is deprecated.
it "returns deprecated" do
expect(SomeTask).to be_deprecated
endHelpers
Including Helper Modules
Include the helper modules in your RSpec configuration or example groups:
RSpec.configure do |config|
config.include CMDx::RSpec::Helpers
endOr include them in specific example groups:
describe MyFeature do
include CMDx::RSpec::Helpers
# Your specs...
endStubs
Helper methods for stubbing CMDx command execution.
Types
it "stubs task executions by type" do
# eg: SomeTask.execute
stub_task_success(SomeTask)
stub_task_skip(SomeTask)
stub_task_fail(SomeTask)
# eg: SomeTask.execute!
stub_task_success!(SomeTask)
stub_task_skip!(SomeTask)
stub_task_fail!(SomeTask)
# Your specs...
end
it "stubs task with arguments" do
# eg: SomeTask.execute(some: "value")
stub_task_success(SomeTask, some: "value")
# eg: SomeTask.execute!(some: "value")
stub_task_skip!(SomeTask, some: "value")
# Your specs...
endOptions
it "stubs task with metadata" do
stub_task_success(SomeTask, metadata: { some: "value" })
# Your specs...
end
it "stubs task with a custom reason" do
stub_task_skip!(SomeTask, reason: "Skipping for a custom reason")
# Your specs...
end
it "stubs task with a custom cause" do
stub_task_fail!(SomeTask, cause: NoMethodError.new("just blow it up"))
# Your specs...
endReset
it "unstubs task executions by type" do
# eg: SomeTask.execute
unstub_task(SomeTask)
# eg: SomeTask.execute!
unstub_task!(SomeTask)
# Your specs...
end
it "unstubs task with arguments" do
# eg: SomeTask.execute(some: "value")
unstub_task(SomeTask, some: "value")
# eg: SomeTask.execute!(some: "value")
unstub_task!(SomeTask, some: "value")
# Your specs...
endMocks
Helper methods for setting expectations on CMDx command execution.
Types
it "mocks task executions by type" do
# eg: SomeTask.execute
expect_task_execution(SomeTask)
expect_no_task_execution(SomeTask)
# eg: SomeTask.execute!
expect_task_execution!(BangCommand)
expect_no_task_execution!(SomeTask)
# Your specs...
end
it "mocks task with arguments" do
# eg: SomeTask.execute(some: "value")
expect_task_execution(SomeTask, some: "value")
expect_no_task_execution(SomeTask, some: "value")
# eg: SomeTask.execute!(some: "value")
expect_task_execution!(SomeTask, some: "value")
expect_no_task_execution!(SomeTask, some: "value")
# Your specs...
endDevelopment
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/drexed/cmdx-rspec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Cmdx::Rspec project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

