Skip to content

Commit 8c37108

Browse files
committed
Merge pull request shakacode#126 from robwise/increase-coverage
Refactor Feature Tests and Increase Coverage
2 parents 51e5a41 + 80ed241 commit 8c37108

File tree

7 files changed

+167
-131
lines changed

7 files changed

+167
-131
lines changed

app/models/comment.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
class Comment < ActiveRecord::Base
2+
validates_presence_of :author
3+
validates_presence_of :text
24
end

spec/features/add_new_comment_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "rails_helper"
2+
require "features/shared/examples"
3+
require "features/shared/contexts"
4+
5+
feature "Add new comment" do
6+
context "from main page", page: :main, js: true do
7+
context "via Horizontal Form", form: :horizontal do
8+
include_examples "New Comment Submission"
9+
end
10+
context "via Inline Form", form: :inline do
11+
include_examples "New Comment Submission"
12+
end
13+
context "via Stacked Form", form: :stacked do
14+
include_examples "New Comment Submission"
15+
end
16+
end
17+
18+
context "from simple page", page: :simple, js: true do
19+
context "via Horizontal Form", form: :horizontal do
20+
include_examples "New Comment Submission"
21+
end
22+
context "via Inline Form", form: :inline do
23+
include_examples "New Comment Submission"
24+
end
25+
context "via the Stacked Form", form: :stacked do
26+
include_examples "New Comment Submission"
27+
end
28+
end
29+
30+
context "from classic page", page: :classic do
31+
background { click_link "New Comment" }
32+
include_examples "New Comment Submission"
33+
end
34+
end

spec/features/comments_spec.rb

-131
This file was deleted.

spec/features/destroy_comment_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "rails_helper"
2+
require "features/shared/contexts"
3+
4+
feature "Destroy a comment", existing_comment: true do
5+
context "from classic page", page: :classic do
6+
let(:comment) { Comment.first }
7+
8+
scenario "clicking destroy link destroys comment" do
9+
click_link "Destroy", href: comment_path(comment)
10+
expect(page).to_not have_css(".comment", text: comment.author)
11+
expect(page).to_not have_css(".comment", text: comment.text)
12+
end
13+
end
14+
end

spec/features/edit_comment_spec.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "rails_helper"
2+
require "features/shared/examples"
3+
require "features/shared/contexts"
4+
5+
feature "Edit a comment", existing_comment: true do
6+
let(:comment) { Comment.first }
7+
8+
context "from classic page", page: :classic do
9+
background { click_link "Edit", match: :first }
10+
11+
context "when edit is submitted" do
12+
let(:edited_name) { "Abraham Lincoln" }
13+
include_context "Form Submitted", name: :edited_name
14+
15+
scenario "comment is updated" do
16+
expect(page).to have_css(".comment", text: :edited_name)
17+
expect(page).to have_success_message
18+
end
19+
end
20+
21+
context "when edit is submitted with blank fields", blank_form_submitted: true do
22+
scenario "comment is not updated" do
23+
expect(page).not_to have_success_message
24+
expect(page).to have_failure_message
25+
expect(page).not_to have_css(".comment", text: "")
26+
end
27+
end
28+
end
29+
end
30+
31+
private
32+
33+
def have_success_message # rubocop:disable Style/PredicateName
34+
have_css("#notice", "Comment was successfully created.")
35+
end
36+
37+
def have_failure_message # rubocop:disable Style/PredicateName
38+
have_css("#error_explanation")
39+
end

spec/features/shared/contexts.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require "rails_helper"
2+
3+
# Pages
4+
shared_context "Main Page", page: :main do
5+
background { visit root_path }
6+
end
7+
shared_context "Simple Page", page: :simple do
8+
background { visit simple_path }
9+
end
10+
shared_context "Classic Page", page: :classic do
11+
background { visit comments_path }
12+
end
13+
14+
# Forms
15+
shared_context "Horizontal Form", form: :horizontal do
16+
background { click_link "Horizontal Form" }
17+
end
18+
shared_context "Inline Form", form: :inline do
19+
background { click_link "Inline Form" }
20+
end
21+
shared_context "Stacked Form", form: :stacked do
22+
background { click_link "Stacked Form" }
23+
end
24+
25+
# Form Submission
26+
shared_context "Form Submitted", form_submitted: true do |name: "Spicoli", text: "dude!"|
27+
let(:hint_name) { "Your Name" }
28+
let(:hint_text) { "Say something using markdown..." }
29+
let(:name) { name }
30+
let(:text) { text }
31+
32+
background do
33+
fill_in hint_name, with: name
34+
fill_in hint_text, with: text
35+
click_button "Post"
36+
end
37+
end
38+
39+
shared_context "Form Submitted with Blank Fields", blank_form_submitted: true do
40+
include_context "Form Submitted", name: "", text: ""
41+
end
42+
43+
# Fixtures
44+
shared_context "Existing Comment", existing_comment: true do
45+
before { Comment.create(author: "John Doe", text: "Hello there!") }
46+
end

spec/features/shared/examples.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "rails_helper"
2+
require "features/shared/contexts"
3+
4+
shared_examples "New Comment Submission" do
5+
context "when the new comment is submitted" do
6+
let(:name) { "John Smith" }
7+
let(:text) { "Hello there!" }
8+
include_context "Form Submitted", name: :name, text: :text
9+
10+
scenario "comment is added" do
11+
expect(page).to have_css(".comment", text: name)
12+
expect(page).to have_css(".comment", text: text)
13+
end
14+
end
15+
16+
context "when the new comment is submmited with blank fields", blank_form_submitted: true do
17+
let!(:comments_count) { all(".comment").size }
18+
19+
scenario "comment is not added" do
20+
expect(page).to have_selector(".comment", count: comments_count)
21+
end
22+
end
23+
24+
context "with iframe text" do
25+
let(:iframe_text) { "<iframe src=\"http://www.w3schools.com\"></iframe>" }
26+
include_context "Form Submitted", text: :iframe_text
27+
28+
scenario "doesn't add an iframe" do
29+
expect(page).not_to have_css("iframe")
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)