mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +00:00
Adding examples of using expect syntax (#2213)
As discussed during the Chef Community Summit 2017 in Seattle, many more technical users wish to use `expect` syntax and wish to see more examples of how to do so with InSpec resources. Signed-off-by: Adam Leff <adam@leff.co>
This commit is contained in:
parent
400aac9350
commit
97a9b3f42a
1 changed files with 39 additions and 2 deletions
|
@ -331,3 +331,40 @@ The tests in `example.rb` can now access this file:
|
|||
it { should be_listening }
|
||||
end
|
||||
end
|
||||
|
||||
# "should" vs. "expect" syntax
|
||||
|
||||
Users familiar with the RSpec testing framework may know that there are two ways to write test statements: `should` and `expect`. The RSpec community decided that `expect` is the preferred syntax. However, InSpec recommends the `should` syntax as it tends to read more easily to those users who are not as technical.
|
||||
|
||||
InSpec will continue to support both methods of writing tests. Consider this `file` test:
|
||||
|
||||
describe file('/tmp/test.txt') do
|
||||
it { should be_file }
|
||||
end
|
||||
|
||||
This can be re-written with `expect` syntax
|
||||
|
||||
describe file('/tmp/test.txt') do
|
||||
it 'should be a file' do
|
||||
expect(subject).to(be_file)
|
||||
end
|
||||
end
|
||||
|
||||
The output of both of the above examples looks like this:
|
||||
|
||||
File /tmp/test.txt
|
||||
✔ should be a file
|
||||
|
||||
In addition, you can make use of the `subject` keyword to further control your output if you choose:
|
||||
|
||||
describe 'test file' do
|
||||
subject { file('/tmp/test.txt') }
|
||||
it 'should be a file' do
|
||||
expect(subject).to(be_file)
|
||||
end
|
||||
end
|
||||
|
||||
... which will render the following output:
|
||||
|
||||
test file
|
||||
✔ should be a file
|
||||
|
|
Loading…
Add table
Reference in a new issue