Skip to content

Commit 29277c8

Browse files
committed
Merge pull request #18 from thecatwasnot/cuc-refactor
Refactor Cucumber step definitions, remove remnants of web_steps.rb in favor of simple features
2 parents 58afa04 + 8663abf commit 29277c8

File tree

6 files changed

+137
-139
lines changed

6 files changed

+137
-139
lines changed
Lines changed: 102 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,146 @@
1-
Given /^no user exists with an email of "(.*)"$/ do |email|
2-
User.find(:first, :conditions => { :email => email }).should be_nil
1+
### UTILITY METHODS ###
2+
def valid_user
3+
@user ||= { :name => "Testy McUserton", :email => "testy@userton.com",
4+
:password => "please", :password_confirmation => "please"}
35
end
46

5-
Given /^I am a user named "([^"]*)" with an email "([^"]*)" and password "([^"]*)"$/ do |name, email, password|
6-
User.new(:name => name,
7-
:email => email,
8-
:password => password,
9-
:password_confirmation => password).save!
7+
def sign_up user
8+
visit '/users/sign_up'
9+
fill_in "Name", :with => user[:name]
10+
fill_in "Email", :with => user[:email]
11+
fill_in "Password", :with => user[:password]
12+
fill_in "Password confirmation", :with => user[:password_confirmation]
13+
click_button "Sign up"
1014
end
1115

12-
Then /^I should be already signed in$/ do
13-
step %{I should see "Logout"}
16+
def sign_in user
17+
visit '/users/sign_in'
18+
fill_in "Email", :with => user[:email]
19+
fill_in "Password", :with => user[:password]
20+
click_button "Sign in"
1421
end
1522

16-
Given /^I am signed up as "(.*)\/(.*)"$/ do |email, password|
17-
step %{I am not logged in}
18-
step %{I go to the sign up page}
19-
step %{I fill in "Email" with "#{email}"}
20-
step %{I fill in "Password" with "#{password}"}
21-
step %{I fill in "Password confirmation" with "#{password}"}
22-
step %{I press "Sign up"}
23-
step %{I should see "You have signed up successfully. If enabled, a confirmation was sent to your e-mail."}
24-
step %{I am logout}
23+
### GIVEN ###
24+
Given /^I am not logged in$/ do
25+
visit '/users/sign_out'
2526
end
2627

27-
Given /^I am logout$/ do
28-
step %{I sign out}
28+
Given /^I am logged in$/ do
29+
sign_up valid_user
2930
end
3031

31-
Given /^I am not logged in$/ do
32-
step %{I sign out}
32+
Given /^I exist as a user$/ do
33+
sign_up valid_user
34+
visit '/users/sign_out'
3335
end
3436

35-
When /^I sign in as "(.*)\/(.*)"$/ do |email, password|
36-
step %{I am not logged in}
37-
step %{I go to the sign in page}
38-
step %{I fill in "Email" with "#{email}"}
39-
step %{I fill in "Password" with "#{password}"}
40-
step %{I press "Sign in"}
37+
Given /^I do not exist as a user$/ do
38+
User.find(:first, :conditions => { :email => valid_user[:email] }).should be_nil
39+
visit '/users/sign_out'
4140
end
4241

43-
Then /^I should be signed in$/ do
44-
step %{I should see "Signed in successfully."}
42+
### WHEN ###
43+
When /^I sign out$/ do
44+
visit '/users/sign_out'
4545
end
4646

47-
When /^I return next time$/ do
48-
step %{I go to the home page}
47+
When /^I sign up with valid user data$/ do
48+
sign_up valid_user
4949
end
5050

51-
Then /^I should be signed out$/ do
52-
step %{I should see "Sign up"}
53-
step %{I should see "Login"}
54-
step %{I should not see "Logout"}
51+
When /^I sign up with an invalid email$/ do
52+
user = valid_user.merge(:email => "notanemail")
53+
sign_up user
5554
end
5655

57-
Then /^I sign out$/ do
58-
visit '/users/sign_out'
56+
When /^I sign up without a confirmed password$/ do
57+
user = valid_user.merge(:password_confirmation => "")
58+
sign_up user
5959
end
6060

61-
When /^I go to the sign in page$/ do
62-
visit '/users/sign_in'
61+
When /^I sign up without a password$/ do
62+
user = valid_user.merge(:password => "")
63+
sign_up user
64+
end
65+
66+
When /^I sign up with a mismatched password confirmation$/ do
67+
user = valid_user.merge(:password_confirmation => "please123")
68+
sign_up user
6369
end
6470

65-
Then /^I should see "([^"]*)"$/ do |text|
66-
page.should have_content(text)
71+
When /^I return to the site$/ do
72+
visit '/'
6773
end
6874

69-
Then /^I should not see "([^"]*)"$/ do |text|
70-
page.should_not have_content(text)
75+
When /^I sign in with a wrong password$/ do
76+
user = valid_user.merge(:password => "wrongpass")
77+
sign_in user
7178
end
7279

73-
Then /^I go to the home page$/ do
74-
visit '/'
80+
When /^I sign in with valid credintials$/ do
81+
sign_in valid_user
82+
end
83+
84+
When /^I edit my account details$/ do
85+
click_link "Edit account"
86+
fill_in "Name", :with => "newname"
87+
fill_in "Current password", :with => valid_user[:password]
88+
click_button "Update"
7589
end
7690

77-
Given /^I am on the home page$/ do
91+
When /^I look at the list of users$/ do
7892
visit '/'
7993
end
8094

81-
Given /^I go to the sign up page$/ do
82-
visit '/users/sign_up'
95+
### THEN ###
96+
Then /^I should be signed in$/ do
97+
page.should have_content "Logout"
98+
page.should_not have_content "Sign up"
99+
page.should_not have_content "Login"
83100
end
84101

85-
Given /^I fill in the following:$/ do |table|
86-
# table is a Cucumber::Ast::Table
87-
table.rows_hash.each do |key, value|
88-
fill_in(key, :with => value)
89-
end
102+
Then /^I should be signed out$/ do
103+
page.should have_content "Sign up"
104+
page.should have_content "Login"
105+
page.should_not have_content "Logout"
90106
end
91107

92-
When /^I press "([^"]*)"$/ do |label|
93-
click_button label
108+
Then /^I should see a succesfull sign up message$/ do
109+
page.should have_content "Welcome! You have signed up successfully."
94110
end
95111

96-
When /^I fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
97-
fill_in(field, :with => value)
112+
Then /^I should see an invalid email message$/ do
113+
page.should have_content "Email is invalid"
98114
end
99115

100-
When /^I go to the homepage$/ do
101-
visit '/'
116+
Then /^I should see a missing password message$/ do
117+
page.should have_content "Password can't be blank"
118+
end
119+
120+
Then /^I should see a missing password confirmation message$/ do
121+
page.should have_content "Password doesn't match confirmation"
122+
end
123+
124+
Then /^I should see a mismatched password message$/ do
125+
page.should have_content "Password doesn't match confirmation"
126+
end
127+
128+
Then /^I should see a signed out message$/ do
129+
page.should have_content "Signed out"
130+
end
131+
132+
Then /^I see an invalid login message$/ do
133+
page.should have_content "Invalid email or password."
134+
end
135+
136+
Then /^I see a successfull sign in message$/ do
137+
page.should have_content "Signed in successfully."
138+
end
139+
140+
Then /^I should see an account edited message$/ do
141+
page.should have_content "You updated your account successfully."
102142
end
103143

104-
When /^I follow "([^"]*)"$/ do |text|
105-
click_link text
144+
Then /^I should see my name$/ do
145+
page.should have_content valid_user[:name]
106146
end

features/users/sign_in.feature

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@ Feature: Sign in
44
Should be able to sign in
55

66
Scenario: User is not signed up
7-
Given I am not logged in
8-
And no user exists with an email of "user@test.com"
9-
When I go to the sign in page
10-
And I sign in as "user@test.com/please"
11-
Then I should see "Invalid email or password."
12-
And I go to the home page
13-
And I should be signed out
7+
Given I do not exist as a user
8+
When I sign in with valid credintials
9+
Then I see an invalid login message
10+
And I should be signed out
1411

1512
Scenario: User enters wrong password
16-
Given I am not logged in
17-
And I am a user named "foo" with an email "user@test.com" and password "please"
18-
When I go to the sign in page
19-
And I sign in as "user@test.com/wrongpassword"
20-
Then I should see "Invalid email or password."
21-
And I go to the home page
22-
And I should be signed out
13+
Given I exist as a user
14+
And I am not logged in
15+
When I sign in with a wrong password
16+
Then I see an invalid login message
17+
And I should be signed out
2318

2419
Scenario: User signs in successfully with email
25-
Given I am not logged in
26-
And I am a user named "foo" with an email "user@test.com" and password "please"
27-
When I go to the sign in page
28-
And I sign in as "user@test.com/please"
29-
Then I should see "Signed in successfully."
30-
And I should be signed in
31-
When I return next time
32-
Then I should be already signed in
20+
Given I exist as a user
21+
And I am not logged in
22+
When I sign in with valid credintials
23+
Then I see a successfull sign in message
24+
When I return to the site
25+
Then I should be signed in

features/users/sign_out.feature

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ Feature: Sign out
44
Should be able to sign out
55

66
Scenario: User signs out
7-
Given I am a user named "foo" with an email "user@test.com" and password "please"
8-
When I sign in as "user@test.com/please"
9-
Then I should be signed in
10-
And I sign out
11-
Then I should see "Signed out"
12-
When I return next time
7+
Given I am logged in
8+
When I sign out
9+
Then I should see a signed out message
10+
When I return to the site
1311
Then I should be signed out

features/users/sign_up.feature

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,24 @@ Feature: Sign up
55

66
Background:
77
Given I am not logged in
8-
And I am on the home page
9-
And I go to the sign up page
108

119
Scenario: User signs up with valid data
12-
And I fill in the following:
13-
| Name | Testy McUserton |
14-
| Email | user@test.com |
15-
| Password | please |
16-
| Password confirmation | please |
17-
And I press "Sign up"
18-
Then I should see "Welcome! You have signed up successfully."
10+
When I sign up with valid user data
11+
Then I should see a succesfull sign up message
1912

2013
Scenario: User signs up with invalid email
21-
And I fill in the following:
22-
| Name | Testy McUserton |
23-
| Email | invalidemail |
24-
| Password | please |
25-
| Password confirmation | please |
26-
And I press "Sign up"
27-
Then I should see "Email is invalid"
14+
When I sign up with an invalid email
15+
Then I should see an invalid email message
2816

2917
Scenario: User signs up without password
30-
And I fill in the following:
31-
| Name | Testy McUserton |
32-
| Email | user@test.com |
33-
| Password | |
34-
| Password confirmation | please |
35-
And I press "Sign up"
36-
Then I should see "Password can't be blank"
18+
When I sign up without a password
19+
Then I should see a missing password message
3720

3821
Scenario: User signs up without password confirmation
39-
And I fill in the following:
40-
| Name | Testy McUserton |
41-
| Email | user@test.com |
42-
| Password | please |
43-
| Password confirmation | |
44-
And I press "Sign up"
45-
Then I should see "Password doesn't match confirmation"
22+
When I sign up without a confirmed password
23+
Then I should see a missing password confirmation message
4624

4725
Scenario: User signs up with mismatched password and confirmation
48-
And I fill in the following:
49-
| Name | Testy McUserton |
50-
| Email | user@test.com |
51-
| Password | please |
52-
| Password confirmation | please1 |
53-
And I press "Sign up"
54-
Then I should see "Password doesn't match confirmation"
26+
When I sign up with a mismatched password confirmation
27+
Then I should see a mismatched password message
5528

features/users/user_edit.feature

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ Feature: Edit User
44
so I can change my username
55

66
Scenario: I sign in and edit my account
7-
Given I am a user named "foo" with an email "user@test.com" and password "please"
8-
When I sign in as "user@test.com/please"
9-
Then I should be signed in
10-
When I follow "Edit account"
11-
And I fill in "Name" with "baz"
12-
And I fill in "Current password" with "please"
13-
And I press "Update"
14-
And I go to the homepage
15-
Then I should see "User: baz"
7+
Given I am logged in
8+
When I edit my account details
9+
Then I should see an account edited message

features/users/user_show.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Feature: Show Users
44
so I can know if the site has users
55

66
Scenario: Viewing users
7-
Given I am a user named "foo" with an email "user@test.com" and password "please"
8-
When I go to the homepage
9-
Then I should see "User: foo"
7+
Given I exist as a user
8+
When I look at the list of users
9+
Then I should see my name

0 commit comments

Comments
 (0)