Skip to content

Commit

Permalink
Convert specs to use expect syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeace committed Apr 4, 2015
1 parent de6815b commit a077533
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 101 deletions.
8 changes: 4 additions & 4 deletions spec/horseman/action_spec.rb
Expand Up @@ -7,23 +7,23 @@
subject {described_class.new(URI.parse('http://www.example.com/path/file.html'))}

it "provides access to full URL" do
subject.url.should eq 'http://www.example.com/path/file.html'
expect(subject.url).to eq 'http://www.example.com/path/file.html'
end

it "provides access to a relative path root" do
subject.relative_root.should eq 'http://www.example.com/path/'
expect(subject.relative_root).to eq 'http://www.example.com/path/'
end
end

context "when given a URL with a query string" do
subject {described_class.new(URI.parse('http://www.example.com/path/file.html?q1=value'))}

it "provides access to full URL" do
subject.url.should eq 'http://www.example.com/path/file.html?q1=value'
expect(subject.url).to eq 'http://www.example.com/path/file.html?q1=value'
end

it "provides access to a relative path root" do
subject.relative_root.should eq 'http://www.example.com/path/'
expect(subject.relative_root).to eq 'http://www.example.com/path/'
end
end
end
64 changes: 32 additions & 32 deletions spec/horseman/browser_spec.rb
Expand Up @@ -6,40 +6,40 @@
subject { described_class.new(connection, js_engine, "http://www.example.com")}

it "saves cookies" do
subject.cookies.should be_empty
expect(subject.cookies).to be_empty

subject.get!
subject.cookies.count.should eq 2
subject.cookies["name1"].should eq "value1"
subject.cookies["name2"].should eq "value2"
expect(subject.cookies.count).to eq 2
expect(subject.cookies["name1"]).to eq "value1"
expect(subject.cookies["name2"]).to eq "value2"

subject.connection.should_receive(:exec_request) do |request|
request["cookie"].should match /\w+=\w+; \w+=\w+/
request["cookie"].should match /name1=value1/
request["cookie"].should match /name2=value2/
expect(subject.connection).to receive(:exec_request) do |request|
expect(request["cookie"]).to match /\w+=\w+; \w+=\w+/
expect(request["cookie"]).to match /name1=value1/
expect(request["cookie"]).to match /name2=value2/
response
end
subject.get!
end

it "empties the cookies when the session is cleared" do
subject.get!
subject.cookies.should_not be_empty
expect(subject.cookies).not_to be_empty
subject.clear_session
subject.cookies.should be_empty
expect(subject.cookies).to be_empty
end

it "stores information about the last response" do
subject.get!
subject.last_action.response.body.should eq html
expect(subject.last_action.response.body).to eq html
end

context "when javascript is enabled" do
subject {described_class.new(connection, js_engine, "http://www.example.com", :enable_js => true)}
it "executes javascript" do
subject.js_engine.should_receive(:execute) do |body, scope|
[%{alert("downloaded");}, %{alert("hello");}, %{alert("no type");}].should include(body)
end.exactly(3).times
expect(subject.js_engine).to receive(:execute) { |body, scope|
expect([%{alert("downloaded");}, %{alert("hello");}, %{alert("no type");}]).to include(body)
}.exactly(3).times

subject.get!
end
Expand All @@ -53,14 +53,14 @@ def build_browser(options)
location = options[:location]
r = response(:location => location)
redirects = 0
r.stub(:code) do
allow(r).to receive(:code) do
code = (redirects >= num_redirects) ? "200" : code
redirects += 1
code
end

c = connection
c.stub(:exec_request) { r }
allow(c).to receive(:exec_request) { r }

described_class.new(c, js_engine, "http://www.example.com")
end
Expand All @@ -70,10 +70,10 @@ def should_redirect(options)

expected_url = options[:expected_url] || "http://www.anotherdomain.com/path"
redirects = 0
browser.connection.should_receive(:build_request).twice do |options|
expect(browser.connection).to receive(:build_request).twice do |options|
browser.connection.url = options[:url]
if redirects > 0
options[:url].should eq expected_url
expect(options[:url]).to eq expected_url
end
redirects += 1
request
Expand Down Expand Up @@ -113,7 +113,7 @@ def should_redirect(options)
context "when posting" do
def describe_url
count = 0
subject.connection.should_receive(:build_request).twice do |options|
expect(subject.connection).to receive(:build_request).twice do |options|
subject.connection.url = options[:url]
yield options[:url] if (count > 0)
count += 1
Expand All @@ -123,7 +123,7 @@ def describe_url
end

def describe_request
subject.connection.should_receive(:exec_request).twice do |request|
expect(subject.connection).to receive(:exec_request).twice do |request|
if (request.method == "POST")
yield request
end
Expand All @@ -143,7 +143,7 @@ def post

it "constructs the correct URL" do
describe_url do |url|
url.should eq "http://www.example.com/action"
expect(url).to eq "http://www.example.com/action"
end
end
end
Expand All @@ -155,7 +155,7 @@ def post

it "constructs the correct URL" do
describe_url do |url|
url.should eq "http://www.anotherdomain.com/action"
expect(url).to eq "http://www.anotherdomain.com/action"
end
end
end
Expand All @@ -167,7 +167,7 @@ def post

it "constructs the correct URL" do
describe_url do |url|
url.should eq "http://www.example.com/path"
expect(url).to eq "http://www.example.com/path"
end
end
end
Expand All @@ -179,7 +179,7 @@ def post

it "does not include unchecked checkboxes" do
describe_request do |request|
request.body.should_not match /name="check"/
expect(request.body).not_to match /name="check"/
end
end
end
Expand All @@ -191,15 +191,15 @@ def post

it "properly sets content type" do
describe_request do |request|
request["Content-Type"].should eq "multipart/form-data; boundary=#{subject.multipart_boundary}"
expect(request["Content-Type"]).to eq "multipart/form-data; boundary=#{subject.multipart_boundary}"
end
end

it "properly encodes form data" do
describe_request do |request|
request.body.should match /\A--#{subject.multipart_boundary}.*--#{subject.multipart_boundary}--\Z/m
request.body.should match /^--#{subject.multipart_boundary}\r\nContent-Disposition: form-data; name="text"\r\n\r\ntext_value/m
request.body.should match /^--#{subject.multipart_boundary}\r\nContent-Disposition: form-data; name="check"\r\n\r\ncheckbox_value/m
expect(request.body).to match /\A--#{subject.multipart_boundary}.*--#{subject.multipart_boundary}--\Z/m
expect(request.body).to match /^--#{subject.multipart_boundary}\r\nContent-Disposition: form-data; name="text"\r\n\r\ntext_value/m
expect(request.body).to match /^--#{subject.multipart_boundary}\r\nContent-Disposition: form-data; name="check"\r\n\r\ncheckbox_value/m
end
end
end
Expand All @@ -211,15 +211,15 @@ def post

it "properly sets content type" do
describe_request do |request|
request["Content-Type"].should eq "application/x-www-form-urlencoded"
expect(request["Content-Type"]).to eq "application/x-www-form-urlencoded"
end
end

it "properly encodes form data" do
describe_request do |request|
request.body.should match /\w+=\w+&\w+=\w+/
request.body.should match /text1=value1/
request.body.should match /text2=value2/
expect(request.body).to match /\w+=\w+&\w+=\w+/
expect(request.body).to match /text1=value1/
expect(request.body).to match /text2=value2/
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/horseman/connection_spec.rb
Expand Up @@ -12,41 +12,41 @@
let(:request) {subject.build_request(:verb => :get)}

it "uses the proper path" do
request.path.should eq "/some/path"
expect(request.path).to eq "/some/path"
end

context "using GET" do
it "uses the proper method" do
request.method.should eq "GET"
expect(request.method).to eq "GET"
end
end

context "using POST" do
let(:request) {subject.build_request(:verb => :post)}

it "uses the proper method" do
request.method.should eq "POST"
expect(request.method).to eq "POST"
end

context "with form data" do
let(:request) {subject.build_request(:verb => :post, :body => "field1=value1&field2=value2")}

it "properly sets request body" do
request.body.should eq "field1=value1&field2=value2"
expect(request.body).to eq "field1=value1&field2=value2"
end
end

context "without form data" do
it "properly sets request body" do
request.body.should be_nil
expect(request.body).to be_nil
end
end
end
end

context "when accessed using http" do
it "does not use SSL" do
subject.http.use_ssl?.should be_falsey
expect(subject.http.use_ssl?).to be_falsey
end
end

Expand All @@ -58,7 +58,7 @@
end

it "uses SSL" do
subject.http.use_ssl?.should be_truthy
expect(subject.http.use_ssl?).to be_truthy
end
end
end
32 changes: 16 additions & 16 deletions spec/horseman/cookies_spec.rb
Expand Up @@ -11,32 +11,32 @@ def test
let(:complex_header) {'name2=value2; Domain=www.example.com; Path=/path; Expires=Sun, 1-Jan-2012 00:00:00 GMT'}

it "starts empty" do
subject.should be_empty
expect(subject).to be_empty
end

it "accepts a single header" do
subject.update(simple_header)['name1'].should eq 'value1'
expect(subject.update(simple_header)['name1']).to eq 'value1'
end

it "accepts multiple headers" do
subject.update([simple_header, complex_header])
subject['name1'].should eq 'value1'
subject['name2'].should eq 'value2'
expect(subject['name1']).to eq 'value1'
expect(subject['name2']).to eq 'value2'
end

it "captures attributes" do
subject.update(complex_header)
subject.get('name2').domain.should eq 'www.example.com'
subject.get('name2').path.should eq '/path'
subject.get('name2').expiration.should eq DateTime.new(2012, 1, 1, 0, 0, 0, 0)
expect(subject.get('name2').domain).to eq 'www.example.com'
expect(subject.get('name2').path).to eq '/path'
expect(subject.get('name2').expiration).to eq DateTime.new(2012, 1, 1, 0, 0, 0, 0)
end

it "accepts an empty array" do
subject.update([]).should be_empty
expect(subject.update([])).to be_empty
end

it "accepts nil" do
subject.update(nil).should be_empty
expect(subject.update(nil)).to be_empty
end

it "raises an exception on an unrecognized header" do
Expand All @@ -45,9 +45,9 @@ def test

it "generates a correct header" do
header = subject.update([simple_header, complex_header]).to_s
header.should match /\w+=\w+; \w+=\w+/
header.should match /name1=value1/
header.should match /name2=value2/
expect(header).to match /\w+=\w+; \w+=\w+/
expect(header).to match /name1=value1/
expect(header).to match /name2=value2/
end

context "with prexisting values" do
Expand All @@ -56,18 +56,18 @@ def test
end

it "returns nil for uninitialized values" do
subject['doesnt_exist'].should be_nil
expect(subject['doesnt_exist']).to be_nil
end

it "merges new values" do
subject.update(complex_header)
subject['name1'].should eq 'other_value'
subject['name2'].should eq 'value2'
expect(subject['name1']).to eq 'other_value'
expect(subject['name2']).to eq 'value2'
end

it "overwrites existing values" do
subject.update(simple_header)
subject['name1'].should eq 'value1'
expect(subject['name1']).to eq 'value1'
end
end

Expand Down

0 comments on commit a077533

Please sign in to comment.