Collection of RSpec matchers for CMDx.
Add this line to your application's Gemfile:
gem 'cmdx-rspec'And then execute:
$ bundle
Or install it yourself as:
$ gem install cmdx-rspec
Asserts that a CMDx task result indicates successful execution.
it "returns success" do
result = SomeTask.execute
expect(result).to be_successful
endAsserts 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`...
)
endAsserts 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`...
)
endAsserts that a CMDx task result has no metadata.
it "returns empty metadata" do
result = SomeTask.execute
expect(result).to have_empty_metadata
endAsserts 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)
endAsserts that a CMDx task result has no context data.
it "returns empty context" do
result = SomeTask.execute
expect(result).to have_empty_context
endAsserts 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)
endAsserts that a CMDx task result indicates the task is deprecated.
it "returns deprecated" do
expect(SomeTask).to be_deprecated
endInclude 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...
endHelper methods for stubbing CMDx command execution.
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...
endit "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...
endit "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...
endHelper methods for setting expectations on CMDx command execution.
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...
endAfter 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.
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.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Cmdx::Rspec project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

