Rails3からRails4へアップグレードしてみましたヘ(^o^)ノ
TurnipとCucumberについても近々書こう
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.0.0'
group :development do
gem 'sqlite3'
gem 'better_errors'
gem 'binding_of_caller'
end
group :test do
gem 'rspec-rails'
gem 'spring'
gem 'turnip'
gem 'guard-rspec'
gem 'guard-spring'
gem 'growl'
gem 'rb-fsevent'
gem 'factory_girl_rails'
gem 'database_cleaner'
end
group :production do
gem 'thin'
gem 'pg'
end
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
gem 'sdoc', require: false
end
gem 'bcrypt-ruby', '~> 3.0.0'
Guardfile
guard :rspec, spring: true, turnip: true do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# # Capybara features specs
# watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
app/models/user.rb
class User < ActiveRecord::Base
has_secure_password
validates :login_name, uniqueness: true
validates_presence_of :login_name, :admin
validates_format_of :login_name, with: /\A[a-z][a-z0-9]*[0-9]\Z/
end
spec/models/user_spec.rb
require 'spec_helper'
describe User do
describe '#validate' do
before :all do
@user = FactoryGirl.create(:user)
end
it "正常に保存できる" do
expect(@user.save).to be true
end
context 'login_name' do
it "login_nameは数字のみは不可" do
@user.login_name = "111"
expect(@user.save).to be false
end
it "login_nameは英字のみは不可" do
@user.login_name = "eee"
expect(@user.save).to be false
end
it "login_nameの先頭は必ず英字" do
@user.login_name = "111aaa"
expect(@user.save).to be false
end
it "login_nameは必須" do
@user.login_name = ""
expect(@user.save).to be false
end
it "login_nameは重複不可" do
@user2 = FactoryGirl.build(:user2)
expect(@user2.save).to be false
end
end
after :all do
User.delete_all
end
end
end
spec/factories/users.rb
FactoryGirl.define do
factory :user, class: User do |f|
f.login_name 'murajun1978'
f.password 'murajun1978'
f.password_confirmation { |u| u.password }
f.admin true
end
factory :user2, class: User do |f|
f.login_name 'murajun1978'
f.password 'sss'
f.password_confirmation { |u| u.password }
f.admin true
end
end
.rspec
--color
--format d
実行結果
User
#validate
正常に保存できる
login_name
login_nameは数字のみは不可
login_nameの先頭は必ず英字
login_nameは重複不可
login_nameは英字のみは不可
login_nameは必須
Finished in 0.10709 seconds
6 examples, 0 failures
validateの正規表現で^や$が使えなくなってたねー
( ̄(エ) ̄)彡☆