From 7d8c56e3f2622a3e692b7adf98a39691f9e42b24 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Wed, 16 Jun 2021 23:20:03 +0900 Subject: [PATCH] refactor test --- spec/agent_spec.cr | 28 +++------------------------- spec/form_spec.cr | 14 ++++++++------ spec/http_spec.cr | 1 - spec/page_spec.cr | 24 ++---------------------- spec/spec_helper.cr | 26 ++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 54 deletions(-) diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index f216454..7a966f8 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -1,6 +1,4 @@ require "./spec_helper" -WebMock.stub(:get, "example.com/") -WebMock.stub(:get, "another_domain.com/") WebMock.stub(:get, "example.com/cookies1").to_return(headers: {"Set-Cookie" => "id=123"}) WebMock.stub(:get, "example.com/cookies2").to_return(headers: {"Set-Cookie" => "name=kanezoh"}) WebMock.stub(:get, "example.com/meta_cookie").to_return(body: @@ -15,30 +13,11 @@ WebMock.stub(:get, "example.com/meta_cookie").to_return(body: BODY ) -WebMock.stub(:get, "html_example.com").to_return(body: -<<-BODY - - - page_title - - -
- - - -
- - -BODY -) -WebMock.stub(:post, "http://html_example.com/post_path"). - with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}). - to_return(body: "success") describe "Mechanize Agent test" do it "can fill and submit form" do agent = Mechanize.new - page = agent.get("http://html_example.com/") + page = agent.get("http://example.com/form") form = page.forms[0] form.field_with("name").value = "foo" form.field_with("email").value = "bar" @@ -55,7 +34,6 @@ describe "Mechanize Agent test" do agent.get("http://example.com/") agent.request_headers["Cookie"].should eq "id=123" end - it "can receive and send multiple cookies" do agent = Mechanize.new # receive cookies1 @@ -87,7 +65,7 @@ describe "Mechanize Agent test" do agent.get("http://example.com/") agent.history.size.should eq 1 agent.history.last.title.should eq "" - agent.get("http://html_example.com/") + agent.get("http://example.com/form") agent.history.size.should eq 2 agent.history.last.title.should eq "page_title" end @@ -95,7 +73,7 @@ describe "Mechanize Agent test" do it "can back previous page" do agent = Mechanize.new agent.get("http://example.com/") - agent.get("http://html_example.com/") + agent.get("http://example.com/form") agent.current_page.title.should eq "page_title" agent.back agent.current_page.title.should eq "" diff --git a/spec/form_spec.cr b/spec/form_spec.cr index 50c4d78..bfc6929 100644 --- a/spec/form_spec.cr +++ b/spec/form_spec.cr @@ -1,8 +1,8 @@ require "./spec_helper" -WebMock.stub(:get, "html_example.com").to_return(body: + +WebMock.stub(:get, "example.com/check_form").to_return(body: <<-BODY - page_title @@ -10,7 +10,7 @@ WebMock.stub(:get, "html_example.com").to_return(body:
- +
@@ -19,7 +19,7 @@ BODY describe "Mechanize Form test" do agent = Mechanize.new - uri = "http://html_example.com/" + uri = "http://example.com/check_form" page = agent.get(uri) form = page.forms.first @@ -59,8 +59,10 @@ describe "Mechanize Form test" do checkbox.click checkbox.checked?.should eq true end - it "returns query value" do - p checkbox.query_value + it "doesn't include request data if checkbox isn't checked" do + form.request_data.should contain("remember_me=on") + checkbox.uncheck + form.request_data.should_not contain("remember_me=") end end end diff --git a/spec/http_spec.cr b/spec/http_spec.cr index 539694d..284e4fd 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -1,5 +1,4 @@ require "./spec_helper" -WebMock.stub(:get, "example.com") WebMock.stub(:get, "http://example.com/?foo=bar&foo1=bar2") WebMock.stub(:post, "http://example.com/post"). with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}). diff --git a/spec/page_spec.cr b/spec/page_spec.cr index b69ff22..bfa32b4 100644 --- a/spec/page_spec.cr +++ b/spec/page_spec.cr @@ -1,24 +1,4 @@ require "./spec_helper" -WebMock.stub(:get, "example.com") -WebMock.stub(:get, "fail_example.com").to_return(status: 500) -WebMock.stub(:get, "body_example.com").to_return(body: "hello") -WebMock.stub(:get, "html_example.com").to_return(body: -<<-BODY - - - - page_title - - -
- - - -
- - -BODY -) describe "Mechanize Page test" do it "return status code of request" do @@ -41,13 +21,13 @@ describe "Mechanize Page test" do agent = Mechanize.new page = agent.get("http://example.com/") page.title.should eq "" - page = agent.get("http://html_example.com") + page = agent.get("http://example.com/form") page.title.should eq "page_title" end it "return page forms" do agent = Mechanize.new - page = agent.get("http://html_example.com") + page = agent.get("http://example.com/form") page.forms.size.should eq 1 page.forms.first.action.should eq "post_path" end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index f3568cc..a0478f2 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,3 +1,29 @@ require "spec" require "webmock" require "../src/mechanize" + + +WebMock.stub(:get, "example.com") +WebMock.stub(:get, "fail_example.com").to_return(status: 500) +WebMock.stub(:get, "body_example.com").to_return(body: "hello") +WebMock.stub(:get, "another_domain.com/") + +WebMock.stub(:get, "example.com/form").to_return(body: +<<-BODY + + + page_title + + +
+ + + +
+ + +BODY +) +WebMock.stub(:post, "example.com/post_path"). + with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}). + to_return(body: "success")