Last active
September 9, 2015 17:14
-
-
Save Rodrigora/0dba6e04be5c60275480 to your computer and use it in GitHub Desktop.
Running ActiveRecord specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/setup' | |
Bundler.setup(:default, :test) | |
require 'yaml' | |
require 'active_record' | |
require 'rspec/matchers' | |
require 'factory_girl' | |
require 'factory_girl/preload' | |
require 'factory_girl/preload/rspec2' | |
ActiveSupport::Dependencies.autoload_paths += %w[./app/models] | |
connection_info = YAML.load_file('config/database.yml')['test'] | |
ActiveRecord::Base.establish_connection(connection_info) | |
require_relative 'support/factories' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :user do | |
sequence(:email) {|n| "email#{n}@example.com" } | |
password 'test' | |
end | |
preload do | |
factory(:john) { create(:user) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rails', '4.2.1' | |
gem 'sqlite3' | |
gem 'sass-rails', '~> 5.0' | |
gem 'uglifier', '>= 1.3.0' | |
gem 'jquery-rails' | |
gem 'turbolinks' | |
gem 'bcrypt' | |
group :development, :test do | |
gem 'rspec-rails' | |
gem 'factory_girl' | |
gem 'factory_girl-preload' | |
gem 'pry-meta' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV['RAILS_ENV'] ||= 'test' | |
require 'spec_helper' | |
require File.expand_path('../../config/environment', __FILE__) | |
require 'rspec/rails' | |
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } | |
ActiveRecord::Migration.maintain_test_schema! | |
RSpec.configure do |config| | |
config.use_transactional_fixtures = true | |
config.infer_spec_type_from_file_location! | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_secure_password | |
validates_format_of :email, with: /\A.+@.+\..+\z/ | |
validates_uniqueness_of :email | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record_helper' | |
RSpec.describe User, type: :model do | |
it 'requires email' do | |
user = User.create(email: '') | |
expect(user.errors[:email]).not_to be_empty | |
end | |
it 'rejects duplicated email' do | |
user = User.create(email: users(:john).email) | |
expect(user.errors[:email]).not_to be_empty | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment