Aruba の Matcher 周りの機能についてまとめます。
サンプル
include_an_object
RSpecの include
matcher とはどう違うのだろう?
require 'spec_helper'
RSpec.describe [2, 4, 'a', 11] do
it { is_expected.to include_an_object( be_odd ) }
it { is_expected.to include_an_object( be_an(String) ) }
it { is_expected.not_to include_an_object( be_between(0, 1).inclusive ) }
it { is_expected.to include_an_object( be_between(0, 2).inclusive ) }
it { is_expected.not_to include_an_object( be_between(0, 2).exclusive ) }
it { is_expected.to include_an_object( be_between(0, 3).exclusive ) }
it { is_expected.to include_an_object( be < 10 ) }
it { is_expected.to include_an_object( eq 'a' ) }
it { is_expected.not_to include_an_object( eq 'b' ) }
end
- 実行結果
$ rspec -fd -c spec/matchers/include_an_object_spec.rb
[2, 4, "a", 11]
should include an object be odd
should include an object be a kind of String
should not include an object be between 0 and 1 (inclusive)
should include an object be between 0 and 2 (inclusive)
should not include an object be between 0 and 2 (exclusive)
should include an object be between 0 and 3 (exclusive)
should include an object be < 10
should include an object eq "a"
should not include an object eq "b"
Finished in 0.00641 seconds (files took 0.22814 seconds to load)
9 examples, 0 failures
have_sub_directory
require 'spec_helper'
RSpec.describe 'have_sub_directory', type: :aruba do
context 'single dir' do
let(:directory) { 'dir' }
let(:sub_directory) { 'sub' }
before(:each) { create_directory(File.join(directory, sub_directory)) }
before(:each) { create_directory(File.join(directory, 'sub2')) }
it { expect(directory).to have_sub_directory sub_directory }
end
context 'multi dir' do
let(:directories) { %w(dir1 dir2) }
let(:sub_directory) { 'sub' }
before(:each) do
directories.each { |d| create_directory(File.join(d, sub_directory)) }
end
it { expect(directories).to all have_sub_directory sub_directory }
end
end
- 実行結果
$ rspec -fd -c spec/matchers/have_sub_directory_spec.rb
have_sub_directory
single dir
should have sub directory "sub"
multi dir
should all have sub directory "sub"
Finished in 0.0108 seconds (files took 0.22751 seconds to load)
2 examples, 0 failures
have_file_content
require 'spec_helper'
RSpec.describe 'have_file_content', type: :aruba do
let(:file) { 'hoge.md' }
let(:content) { 'hoge hige hage' }
before(:each) { write_file(file, content) }
it { expect(file).to have_file_content content }
it { expect(file).to have_file_content /h.ge/ }
it { expect(file).to have_file_content end_with('ge') }
end
- 実行結果
$ rspec -fd -c spec/matchers/have_file_content_spec.rb
have_file_content
should have file content: "hoge hige hage"
should have file content: /h.ge/
should have file content: end with "ge"
Finished in 0.01209 seconds (files took 0.22188 seconds to load)
3 examples, 0 failures
be_an_existing_executable
require 'spec_helper'
RSpec.describe 'be_an_existing_executable', type: :aruba do
context 'executable' do
let(:file) { 'file1.txt' }
before(:each) { touch(file) }
before(:each) { chmod(0755, file) }
it { expect(file).to be_an_existing_executable }
end
context 'can not executable' do
let(:file) { 'file2.txt' }
before(:each) { touch(file) }
before(:each) { chmod(0444, file) }
it { expect(file).not_to be_an_existing_executable }
end
end
- 実行結果
$ rspec -fd -c spec/matchers/be_an_existing_executable_spec.rb
be_an_existing_executable
executable
should be an existing executable
can not executable
should not be an existing executable
Finished in 0.00713 seconds (files took 0.22001 seconds to load)
2 examples, 0 failures
be_an_existing_file
require 'spec_helper'
RSpec.describe 'be_an_existing_file', :type => :aruba do
context 'exist' do
let(:file) { 'hoge.txt' }
before(:each) { touch(file) }
it { expect(file).to be_an_existing_file }
end
context 'not exist' do
let(:file) { 'hoge.txt' }
it { expect(file).not_to be_an_existing_file }
end
end
- 実行結果
rspec -fd -c spec/matchers/be_an_existing_file_spec.rb
be_an_existing_file
exist
should be an existing file
not exist
should not be an existing file
Finished in 0.0073 seconds (files took 0.21953 seconds to load)
2 examples, 0 failures
have_file_size
require 'spec_helper'
RSpec.describe 'have_file_size', :type => :aruba do
let(:file) { 'hoge.txt' }
let(:size) { 11 }
before(:each) { write_fixed_size_file(file, size) }
it { expect(file).to have_file_size size }
end
- 実行結果
$ rspec -fd -c spec/matchers/have_file_size_spec.rb
have_file_size
should have file size 11
Finished in 0.00449 seconds (files took 0.22108 seconds to load)
1 example, 0 failures
timeout
require 'spec_helper'
RSpec.describe 'timeout', type: :aruba do
context 'timeout' do
before(:each) { aruba.config.exit_timeout = 1 }
before(:each) { run('sleep 2') }
it { expect(last_command_started).to run_too_long }
end
context 'in time' do
before(:each) { aruba.config.exit_timeout = 4 }
before(:each) { run('sleep 2') }
it { expect(last_command_started).not_to run_too_long }
end
end
- 実行結果
$ rspec -fd -c spec/matchers/timeout_spec.rb
timeout
timeout
should run too long
in time
should not run too long
Finished in 3.22 seconds (files took 0.21704 seconds to load)
2 examples, 0 failures
be_an_existing_path
require 'spec_helper'
RSpec.describe 'be_an_existing_path', :type => :aruba do
context 'exist' do
let(:path) { 'hoge.txt' }
before(:each) { touch(path) }
it { expect(path).to be_an_existing_path }
end
context 'not exist' do
let(:path) { 'hoge.txt' }
it { expect(path).not_to be_an_existing_path }
end
end
- 実行結果
$ rspec -fd -c spec/matchers/be_an_existing_path_spec.rb
be_an_existing_path
exist
should be an existing path
not exist
should not be an existing path
Finished in 0.00685 seconds (files took 0.21496 seconds to load)
2 examples, 0 failures
have_permissions
require 'spec_helper'
RSpec.describe 'have_permissions', :type => :aruba do
let(:file) { 'hoge.txt' }
let(:permissions) { 0544 }
before(:each) { touch(file) }
before(:each) { chmod(permissions, file) }
it { expect(file).to have_permissions permissions }
it { expect(file).not_to have_permissions 777 }
end
- 実行結果
$ rspec -fd -c spec/matchers/have_permissions_spec.rb
have_permissions
should have permissions 356
should not have permissions 777
Finished in 0.00732 seconds (files took 0.20833 seconds to load)
2 examples, 0 failures
be_an_absolute_path
require 'spec_helper'
RSpec.describe 'be_an_absolute_path', :type => :aruba do
context 'absolute' do
let(:path) { '/path/to/hoge.txt' }
it { expect(path).to be_an_absolute_path }
end
context 'relative' do
let(:path) { 'hoge.txt' }
it { expect(path).not_to be_an_absolute_path }
end
end
- 実行結果
$ rspec -fd -c spec/matchers/be_an_absolute_path_spec.rb
be_an_absolute_path
absolute
should be an absolute path
relative
should not be an absolute path
Finished in 0.0083 seconds (files took 0.23092 seconds to load)
2 examples, 0 failures
外部資料
-
include_an_object
Matcher - Aruba - docs - Check if directory has given sub directories - Aruba - docs
- Check if file has content - Aruba - docs
- Check if path exists and is an executable file - Aruba - docs
- Check if path exists and is file - Aruba - docs
- Check if path has size - Aruba - docs
- Check if a timeout occured during command execution - Aruba - docs
- Check if path exists - Aruba - docs
- Check if path has permissions in filesystem - Aruba - docs
- Check if path is absolute - Aruba - docs