Browse files

Added tests for the member model with RSpec, Factory Girl and Shoulda.

Added validations to the member model.
  • Loading branch information...
1 parent 6997b55 commit 431b70db1fb37fb1c61c27ba97131a8084cf55d5 @matheusmoreira committed Apr 14, 2011
View
1 .rspec
@@ -0,0 +1 @@
+--colour
View
7 Gemfile
@@ -25,3 +25,10 @@ group :development do
# Compass CSS statistics
gem 'css_parser'
end
+
+group :development, :test do
+
+ gem 'rspec-rails'
+ gem 'factory_girl_rails'
+ gem 'shoulda'
+end
View
22 Gemfile.lock
@@ -40,8 +40,13 @@ GEM
bcrypt-ruby (~> 2.1.2)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
+ diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
+ factory_girl (1.3.3)
+ factory_girl_rails (1.0.1)
+ factory_girl (~> 1.3)
+ railties (>= 3.0.0)
haml (3.0.25)
i18n (0.5.0)
mail (2.2.15)
@@ -71,6 +76,20 @@ GEM
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.5.0)
+ rspec-rails (2.5.0)
+ actionpack (~> 3.0)
+ activesupport (~> 3.0)
+ railties (~> 3.0)
+ rspec (~> 2.5.0)
+ shoulda (2.11.3)
sqlite3 (1.3.3)
thor (0.14.6)
treetop (1.4.9)
@@ -89,7 +108,10 @@ DEPENDENCIES
css_parser
decent_exposure
devise
+ factory_girl_rails
haml
rails
+ rspec-rails
+ shoulda
sqlite3
will_paginate (= 3.0.pre2)
View
18 app/models/member.rb
@@ -9,7 +9,23 @@ class Member < ActiveRecord::Base
:lockable,
:validatable
- attr_accessible :name, :email, :password, :password_confirmation, :remember_me
+ # Devise already validates presence of email and password
+ validates_presence_of :name
+
+ validates_format_of :name, :with => /^[\d\w\s]+$/
+
+ # Devise already validates uniqueness of email
+ validates_uniqueness_of :name
+
+ validates_length_of :name, :minimum => 1
+
+ validates_length_of :email, :minimum => 5 # "x@y.z".length
+
+ attr_accessible :name,
+ :email,
+ :password,
+ :password_confirmation,
+ :remember_me
def self.per_page
50
View
5 app/views/devise/registrations/edit.html.haml
@@ -6,6 +6,11 @@
= devise_error_messages!
%p
+ = f.label :name
+ %br/
+ = f.text_field :name, class: 'title'
+
+ %p
= f.label :email
%br/
= f.email_field :email
View
1 config/environments/development.rb
@@ -29,5 +29,6 @@
# Send mail to /tmp/mails.
ActionMailer::Base.delivery_method = :file
+
end
View
5 config/environments/test.rb
@@ -1,4 +1,5 @@
Forumby::Application.configure do
+
# Settings specified here will take precedence over those in config/application.rb
# The test environment is used exclusively to run your application's
@@ -32,4 +33,8 @@
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr
+
+ # Default URL for testing.
+ config.action_mailer.default_url_options = { :host => 'localhost:3000' }
+
end
View
2 config/initializers/devise.rb
@@ -4,7 +4,7 @@
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in DeviseMailer.
- config.mailer_sender = "no-reply@forumby.com"
+ config.mailer_sender = "no-reply@forumby.net"
# Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer"
View
8 spec/factories/member.rb
@@ -0,0 +1,8 @@
+Factory.define :member do |f|
+
+ f.name 'test member'
+ f.email 'test_member@forumby.net'
+ f.password 'test member'
+ f.password_confirmation 'test member'
+
+end
View
34 spec/models/member_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe Member do
+
+ subject { Factory(:member) }
+
+ it { should validate_presence_of(:name) }
+ it { should validate_presence_of(:email) }
+ it { should validate_presence_of(:password) }
+
+ it { should ensure_length_of(:name).is_at_least(1) }
+ it { should ensure_length_of(:email).is_at_least(5) }
+
+ it { should_not allow_mass_assignment_of(:id) }
+ it { should_not allow_mass_assignment_of(:moderator) }
+ it { should_not allow_mass_assignment_of(:administrator) }
+ it { should_not allow_mass_assignment_of(:banned) }
+
+ it { should validate_uniqueness_of(:name) }
+ it { should validate_uniqueness_of(:email) }
+
+ it 'should save member with factory parameters' do
+ member = Factory.build :member
+ member.save
+ member.should be_persisted
+ end
+
+ it 'should not save member whose password does not match password confirmation' do
+ member = Factory.build :member, :password_confirmation => 'DOES NOT MATCH'
+ member.save
+ member.should_not be_persisted
+ end
+
+end
View
12 spec/spec_helper.rb
@@ -0,0 +1,12 @@
+ENV["RAILS_ENV"] ||= 'test'
+require File.expand_path("../../config/environment", __FILE__)
+require 'rspec/rails'
+
+Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
+
+RSpec.configure do |config|
+
+ config.mock_with :rspec
+ config.use_transactional_fixtures = true
+
+end
View
11 test/fixtures/members.yml
@@ -1,11 +0,0 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-# This model initially had no columns defined. If you add columns to the
-# model remove the '{}' from the fixture names and add the columns immediately
-# below each fixture, per the syntax in the comments below
-#
-one: {}
-# column: value
-#
-two: {}
-# column: value
View
9 test/functional/forumby_controller_test.rb
@@ -1,9 +0,0 @@
-require 'test_helper'
-
-class ForumbyControllerTest < ActionController::TestCase
-
- test "should get home" do
- get :home
- assert_response :success
- end
-end
View
14 test/functional/members_controller_test.rb
@@ -1,14 +0,0 @@
-require 'test_helper'
-
-class MembersControllerTest < ActionController::TestCase
-
- test "should get index" do
- get :index
- assert_response :success
- end
-
- test "should get show" do
- get :show
- assert_response :success
- end
-end
View
9 test/performance/browsing_test.rb
@@ -1,9 +0,0 @@
-require 'test_helper'
-require 'rails/performance_test_help'
-
-# Profiling results for each test method are written to tmp/performance.
-class BrowsingTest < ActionDispatch::PerformanceTest
- def test_homepage
- get '/'
- end
-end
View
13 test/test_helper.rb
@@ -1,13 +0,0 @@
-ENV["RAILS_ENV"] = "test"
-require File.expand_path('../../config/environment', __FILE__)
-require 'rails/test_help'
-
-class ActiveSupport::TestCase
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
- #
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
- # -- they do not yet inherit this setting
- fixtures :all
-
- # Add more helper methods to be used by all tests here...
-end
View
4 test/unit/helpers/forumby_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class ForumbyHelperTest < ActionView::TestCase
-end
View
4 test/unit/helpers/members_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class MembersHelperTest < ActionView::TestCase
-end
View
8 test/unit/member_test.rb
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class MemberTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end

0 comments on commit 431b70d

Please sign in to comment.