start using rspec for tests

This commit is contained in:
wvengen 2013-07-12 20:06:49 +02:00
parent f57038ff56
commit 7fa8193010
5 changed files with 158 additions and 8 deletions

View File

@ -52,10 +52,6 @@ group :development do
gem 'better_errors' gem 'better_errors'
gem 'binding_of_caller' gem 'binding_of_caller'
# Re-enable rails benchmarker/profiler
gem 'ruby-prof'
gem 'test-unit'
# Get infos when not using proper eager loading # Get infos when not using proper eager loading
gem 'bullet' gem 'bullet'
@ -69,3 +65,8 @@ group :development do
# Avoid having content-length warnings # Avoid having content-length warnings
gem 'thin' gem 'thin'
end end
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails', '~> 4.0'
end

View File

@ -81,6 +81,7 @@ GEM
coffee-script-source (1.3.3) coffee-script-source (1.3.3)
commonjs (0.2.6) commonjs (0.2.6)
daemons (1.1.9) daemons (1.1.9)
diff-lcs (1.2.4)
erubis (2.7.0) erubis (2.7.0)
eventmachine (1.0.3) eventmachine (1.0.3)
exception_notification (2.6.1) exception_notification (2.6.1)
@ -88,6 +89,11 @@ GEM
execjs (1.4.0) execjs (1.4.0)
multi_json (~> 1.0) multi_json (~> 1.0)
expression_parser (0.9.0) expression_parser (0.9.0)
factory_girl (4.2.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.2.1)
factory_girl (~> 4.2.0)
railties (>= 3.0.0)
haml (3.1.7) haml (3.1.7)
haml-rails (0.3.5) haml-rails (0.3.5)
actionpack (>= 3.1, < 4.1) actionpack (>= 3.1, < 4.1)
@ -195,7 +201,17 @@ GEM
redis-namespace (~> 1.2) redis-namespace (~> 1.2)
sinatra (>= 0.9.2) sinatra (>= 0.9.2)
vegas (~> 0.1.2) vegas (~> 0.1.2)
ruby-prof (0.11.2) rspec-core (2.14.2)
rspec-expectations (2.14.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.1)
rspec-rails (2.14.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
ruby-rc4 (0.1.5) ruby-rc4 (0.1.5)
sass (3.2.1) sass (3.2.1)
sass-rails (3.2.5) sass-rails (3.2.5)
@ -222,7 +238,6 @@ GEM
rack (~> 1.0) rack (~> 1.0)
tilt (~> 1.1, != 1.3.0) tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6) sqlite3 (1.3.6)
test-unit (2.5.3)
therubyracer (0.10.2) therubyracer (0.10.2)
libv8 (~> 3.3.10) libv8 (~> 3.3.10)
thin (1.5.1) thin (1.5.1)
@ -271,6 +286,7 @@ DEPENDENCIES
coffee-rails (~> 3.2.1) coffee-rails (~> 3.2.1)
daemons daemons
exception_notification exception_notification
factory_girl_rails (~> 4.0)
haml-rails haml-rails
inherited_resources inherited_resources
jquery-rails jquery-rails
@ -283,13 +299,12 @@ DEPENDENCIES
quiet_assets quiet_assets
rails (~> 3.2.9) rails (~> 3.2.9)
resque resque
ruby-prof rspec-rails
sass-rails (~> 3.2.3) sass-rails (~> 3.2.3)
simple-navigation simple-navigation
simple-navigation-bootstrap simple-navigation-bootstrap
simple_form simple_form
sqlite3 sqlite3
test-unit
therubyracer therubyracer
thin thin
twitter-bootstrap-rails twitter-bootstrap-rails

33
spec/factories/user.rb Normal file
View File

@ -0,0 +1,33 @@
require 'factory_girl'
FactoryGirl.define do
factory :user do
sequence(:nick) { |n| "user#{n}"}
first_name 'John'
email { "#{nick}@foodcoop.test" }
password { new_random_password }
factory :admin do
sequence(:nick) { |n| "admin#{n}" }
first_name 'Administrator'
after :create do |user, evaluator|
FactoryGirl.create :workgroup, role_admin: true, user_ids: [user.id]
end
end
end
factory :group do
sequence(:name) {|n| "Group ##{n}"}
factory :workgroup do
type ''
end
factory :ordergroup do
type 'Ordergroup'
sequence(:name) {|n| "Order group ##{n}"}
end
end
end

63
spec/models/user_spec.rb Normal file
View File

@ -0,0 +1,63 @@
require 'spec_helper'
describe User do
it 'is correctly created' do
user = FactoryGirl.create :user,
nick: 'johnnydoe', first_name: 'Johnny', last_name: 'DoeBar',
email: 'johnnydoe@foodcoop.test', phone: '+1234567890'
user.nick.should == 'johnnydoe'
user.first_name.should == 'Johnny'
user.last_name.should == 'DoeBar'
user.name.should == 'Johnny DoeBar'
user.email.should == 'johnnydoe@foodcoop.test'
user.phone.should == '+1234567890'
end
describe 'does not have the role' do
let(:user) { FactoryGirl.create :user }
it 'admin' do user.role_admin?.should be_false end
it 'finance' do user.role_finance?.should be_false end
it 'article_meta' do user.role_article_meta?.should be_false end
it 'suppliers' do user.role_suppliers?.should be_false end
it 'orders' do user.role_orders?.should be_false end
end
describe do
let(:user) { FactoryGirl.create :user, password: 'blahblah' }
it 'can authenticate with correct password' do
User.authenticate(user.nick, 'blahblah').should be_true
end
it 'can not authenticate with incorrect password' do
User.authenticate(user.nick, 'foobar').should be_nil
end
it 'can not set a password without confirmation' do
user.password = 'abcdefghij'
user.should_not be_valid
end
it 'can not set a password without matching confirmation' do
user.password = 'abcdefghij'
user.password_confirmation = 'foobarxyz'
user.should_not be_valid
end
it 'can set a password with matching confirmation' do
user.password = 'abcdefghij'
user.password_confirmation = 'abcdefghij'
user.should be_valid
end
it 'has a unique nick' do
FactoryGirl.build(:user, nick: user.nick, email: "x-#{user.email}").should_not be_valid
end
it 'has a unique email' do
FactoryGirl.build(:user, email: "#{user.email}").should_not be_valid
end
end
describe 'admin' do
let(:user) { FactoryGirl.create :admin }
it 'default admin role' do user.role_admin?.should be_true end
end
end

38
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,38 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end