Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update tests

  • Loading branch information...
commit 3b455f37458a37f2f5be3b51547e53932f065125 1 parent b670716
@changs authored
View
2  app/models/user.rb
@@ -29,7 +29,7 @@ class User < ActiveRecord::Base
validates :fullname, presence: true, length: { maximum: 30 }
validates :email, presence: true, format: { with: email_regex },
uniqueness: { case_sensitive: false }
- validates :password, length: { in: 5..25 }
+ validates :password, length: { in: 5..25 }, :on => :create
def generate_token(column)
begin
View
2  app/views/items/_form.html.erb
@@ -15,7 +15,7 @@
<%= f.submit "Null", class: "input_submit" %>
</div>
<% end %>
-
+<small> You can type only correct letters. Type 'backspace' for help</small>
<script>
var answer = "<%= @item.answer %>";
var answer_box = $('#item_answer')
View
22 spec/factories.rb
@@ -1,12 +1,14 @@
-Factory.define :user do |f|
- f.sequence(:username) { |n| "john#{n}" }
- f.fullname 'Doe'
- f.sequence(:email) { |n| "test#{n}@example.com" }
- f.password 'foobar'
-end
+FactoryGirl.define do
+ factory :user do
+ sequence(:username) { |n| "john#{n}" }
+ fullname 'Doe'
+ sequence(:email) { |n| "test#{n}@example.com" }
+ password 'foobar'
+ end
-Factory.define :item do |f|
- f.sequence(:question) { |n| "Question #{n}" }
- f.sequence(:answer) { |n| "Answer #{n}" }
- f.association :user
+ factory :item do
+ sequence(:question) { |n| "Question #{n}" }
+ sequence(:answer) { |n| "Answer #{n}" }
+ user
+ end
end
View
16 spec/models/item_relationship_spec.rb
@@ -1,16 +0,0 @@
-# == Schema Information
-#
-# Table name: item_relationships
-#
-# id :integer not null, primary key
-# parent_id :integer
-# child_id :integer
-# created_at :datetime
-# updated_at :datetime
-#
-
-require 'spec_helper'
-
-describe ItemRelationship do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
16 spec/models/relationship_spec.rb
@@ -1,16 +0,0 @@
-# == Schema Information
-#
-# Table name: relationships
-#
-# id :integer not null, primary key
-# follower_id :integer
-# followed_id :integer
-# created_at :datetime
-# updated_at :datetime
-#
-
-require 'spec_helper'
-
-describe Relationship do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
8 spec/requests/items_spec.rb
@@ -12,7 +12,7 @@
describe "GET /review" do
it "it should show message when there is no items for today" do
visit '/review'
- page.should have_css("div.notice")
+ page.should have_css("div.note_notice")
current_path.should eq(user_path(@user))
end
@@ -21,7 +21,7 @@
fill_in "item_question", with: 'Rower'
fill_in "item_answer", with: 'Bicycle'
click_button "Add item"
- page.should have_css("div.success")
+ page.should have_css("div.note_success")
end
it "user can't add an item given invalid attributes" do
@@ -37,8 +37,8 @@
item.review_at = 1.day.ago
item.save
visit '/review'
- page.should have_content("Question 1")
- page.should have_content("Answer 1")
+ page.should have_content(item.question)
+ page.should have_content(item.answer)
end
end
end
View
2  spec/requests/session_spec.rb
@@ -48,7 +48,7 @@
it "should show new_user_form on root_path when no-one is signed in" do
visit root_path
- page.should have_css("div.new_user_form")
+ page.should have_content("Sign up")
end
it "should not show new_user_form on root_path when signed in" do
View
182 spec/requests/users_spec.rb
@@ -1,54 +1,146 @@
require 'spec_helper'
-describe "Users" do
- describe "get /signup" do
- it "should not create account with invalid parameters" do
- lambda do
- visit signup_path
- fill_in "Fullname", with: ""
- fill_in "Email", with: ""
- fill_in "Password", with: ""
- fill_in "Username", with: ""
- click_button "Create my account"
- page.should have_css("div#error_explanation")
- end.should_not change(User, :count)
- end
-
- it "should create account with valid parameters" do
- lambda do
- visit signup_path
- fill_in "Fullname", with: "Foo Bar"
- fill_in "Email", with: "foo@example.com"
+describe "User pages" do
+
+ subject { page }
+
+ describe "signup page" do
+ before { visit signup_path }
+ it { should have_selector('h2', text: "Create a new user") }
+ end
+
+ describe "signup" do
+ before { visit signup_path }
+
+ describe "with invalid information" do
+ it "should not create a user" do
+ expect { click_button "Create my account" }.not_to change(User, :count)
+ end
+ describe "error messages" do
+ before { click_button "Create my account" }
+ it { should have_content('You are almost done') }
+ end
+ end
+
+ describe "with valid information" do
+ before do
+ fill_in "Fullname", with: "Example User"
+ fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
- fill_in "Username", with: "foo"
- click_button "Create my account"
- page.should have_content("Your account has been created")
- end.should change(User, :count).by(1)
+ fill_in "Username", with: "User"
+ end
+
+ it "should create a user" do
+ expect { click_button "Create my account" }.to change(User, :count).by(1)
+ end
+
+ describe "after saving the user" do
+ before { click_button "Create my account" }
+ let(:user) { User.find_by_email('user@example.com') }
+
+ it { should have_content("#{user.username} - #{user.fullname}") }
+ it { should have_selector("div.note_success", text: "created") }
+ it { should have_link("Sign out") }
+ end
end
+ end
- it "should edit user's account with valid parameters" do
- user = Factory(:user)
- signin user
+ describe "edit" do
+ let(:user) { FactoryGirl.create(:user) }
+ before do
+ sign_in user
visit edit_user_path(user)
- fill_in "Password", with: user.password
- fill_in "Username", with: "new_username"
- click_button "Save changes"
- page.should have_content("updated")
- page.should have_content("new_username")
- current_path.should eq(user_path(user))
- end
-
- it "should sign in a user after sign up" do
- lambda do
- visit signup_path
- fill_in "Fullname", with: "Foo Bar"
- fill_in "Email", with: "foo@example.com"
- fill_in "Password", with: "foobar"
- fill_in "Username", with: "foo"
- click_button "Create my account"
- page.should have_content("Your account has been created")
- page.should have_content("My page")
- end.should change(User, :count).by(1)
+ end
+
+ describe "page" do
+ it { should have_content("Edit") }
+ end
+
+ describe "with invalid information" do
+ before do
+ fill_in "Username", with: " "
+ click_button "Save changes"
+ end
+ it { should have_selector("div#error_explanation") }
+ end
+
+ describe "with valid information" do
+ let(:new_username) { "NewUsername" }
+ before do
+ fill_in "Username", with: new_username
+ click_button "Save changes"
+ end
+
+ it { should have_selector("div.note_success") }
+ specify { user.reload.username.should == new_username }
+ end
+ end
+
+ describe "profile" do
+ let(:user) { FactoryGirl.create(:user) }
+ let!(:i1) { FactoryGirl.create(:item, user: user) }
+ let!(:i2) { FactoryGirl.create(:item, user: user) }
+
+ before do
+ sign_in user
+ visit user_path(user)
+ end
+
+ it { should have_content("#{user.username} - #{user.fullname}")}
+ it { should have_content("Items: 2") }
+
+ describe "follow/unfollow buttons" do
+ let(:user2) { FactoryGirl.create(:user) }
+ let!(:i3) { FactoryGirl.create(:item, user: user2) }
+ let!(:i4) { FactoryGirl.create(:item, user: user2) }
+
+ describe "following a user" do
+ before do
+ visit user_path(user2)
+ end
+
+ it "should increment the followed user count" do
+ expect do
+ click_button "Follow"
+ end.to change(user.following, :count).by(1)
+ end
+
+ it "should increment user2's followers count" do
+ expect do
+ click_button "Follow"
+ end.to change(user2.followers, :count).by(1)
+ end
+
+ describe "toggling the button" do
+ before { click_button "Follow" }
+ it { should have_selector('input', value: "Unfollow") }
+ end
+ end
+
+ describe "unfollowing a user" do
+ before do
+ user.follow!(user2)
+ visit user_path(user2)
+ end
+
+ it "should decrement the followed user count" do
+ expect do
+ click_button "Unfollow"
+ end.to change(user.following, :count).by(-1)
+ end
+
+ it "should decrement user2's followers count" do
+ expect do
+ click_button "Unfollow"
+ end.to change(user2.followers, :count).by(-1)
+ end
+
+ describe "toggling the button" do
+ before { click_button "Unfollow" }
+ it { should have_selector('input', value: 'Follow') }
+ end
+ end
+
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.