WpItem::Existable specs
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe WpItem do
|
||||
it_behaves_like 'WpItem::Existable'
|
||||
|
||||
subject(:wp_item) { WpItem.new(uri, options) }
|
||||
let(:uri) { URI.parse('http://example.com') }
|
||||
let(:options) { {} }
|
||||
|
||||
80
spec/shared_examples/wp_item_existable.rb
Normal file
80
spec/shared_examples/wp_item_existable.rb
Normal file
@@ -0,0 +1,80 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
shared_examples 'WpItem::Existable' do
|
||||
|
||||
describe '#exists?' do
|
||||
context 'when the response is supplied' do
|
||||
let(:response) { Typhoeus::Response.new }
|
||||
|
||||
it 'does not create a request' do
|
||||
Browser.instance.should_not_receive(:get)
|
||||
subject.stub(:exists_from_response?).and_return(true)
|
||||
|
||||
subject.exists?({}, response).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the response is not supplied' do
|
||||
it 'creates a request' do
|
||||
Browser.instance.should_receive(:get)
|
||||
subject.stub(:exists_from_response?).and_return(false)
|
||||
|
||||
subject.exists?.should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#exists_from_response?' do
|
||||
let(:exists_options) { {} }
|
||||
let(:body) { 'hello world!' }
|
||||
|
||||
after do
|
||||
response = Typhoeus::Response.new(@resp_opt)
|
||||
subject.send(:exists_from_response?, response, exists_options).should == @expected
|
||||
end
|
||||
|
||||
context 'when invalid response.code' do
|
||||
it 'returns false' do
|
||||
@resp_opt = { code: 500 }
|
||||
@expected = false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the body hash = homepage_hash or error_404_hash' do
|
||||
let(:exists_options) { { homepage_hash: Digest::MD5.hexdigest(body) } }
|
||||
|
||||
it 'returns false' do
|
||||
@resp_opt = { code: 200, body: body }
|
||||
@expected = false
|
||||
end
|
||||
end
|
||||
|
||||
context 'w/o exclude_content' do
|
||||
[200, 301, 302, 401, 403].each do |code|
|
||||
it "returns true on #{code}" do
|
||||
@resp_opt = { code: code, body: '' }
|
||||
@expected = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with exclude_content' do
|
||||
let(:exists_options) { { exclude_content: %r{world!} } }
|
||||
|
||||
context 'when the body match' do
|
||||
it 'returns false' do
|
||||
@resp_opt = { code: 200, body: body }
|
||||
@expected = false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the body does not match' do
|
||||
it 'returns true' do
|
||||
@resp_opt = { code: 200, body: 'hello dude!' }
|
||||
@expected = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -30,11 +30,15 @@ SPEC_DIR = ROOT_DIR + '/spec'
|
||||
SPEC_LIB_DIR = SPEC_DIR + '/lib'
|
||||
SPEC_CACHE_DIR = SPEC_DIR + '/cache'
|
||||
SPEC_FIXTURES_DIR = SPEC_DIR + '/samples'
|
||||
SHARED_EXAMPLES_DIR = SPEC_DIR + '/shared_examples'
|
||||
SPEC_FIXTURES_CONF_DIR = SPEC_FIXTURES_DIR + '/conf'
|
||||
SPEC_FIXTURES_WP_VERSIONS_DIR = SPEC_FIXTURES_DIR + '/wp_versions'
|
||||
|
||||
MODELS_FIXTURES = SPEC_FIXTURES_DIR + '/common/models'
|
||||
|
||||
# Load all the shared examples
|
||||
require_files_from_directory(SHARED_EXAMPLES_DIR)
|
||||
|
||||
def count_files_in_dir(absolute_dir_path, files_pattern = '*')
|
||||
Dir.glob(File.join(absolute_dir_path, files_pattern)).count
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user