Skip to content

Commit b61840c

Browse files
authored
Added some specs (#3)
* Added some specs
1 parent 7e76c81 commit b61840c

9 files changed

Lines changed: 189 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ spec/reports
1515
test/tmp
1616
test/version_tmp
1717
tmp
18+
.byebug_history

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: ruby
2+
sudo: false
3+
branches:
4+
rvm:
5+
- 2.2.7
6+
- 2.3.4
7+
- 2.4.1

Rakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
task default: :spec

easy_reference_data.gemspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ Gem::Specification.new do |gem|
1818
gem.require_paths = ["lib"]
1919

2020
gem.add_runtime_dependency 'rails', '>= 3.0.0'
21+
22+
gem.add_development_dependency 'rspec', '~> 3.6.0'
23+
gem.add_development_dependency 'sqlite3', '~> 1.3'
24+
gem.add_development_dependency 'database_cleaner', '~> 1.6'
2125
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'spec_helper'
2+
require 'easy/reference_data/refresh'
3+
4+
RSpec.describe Easy::ReferenceData do
5+
6+
describe ".refresh" do
7+
8+
context "with a unique attribue" do
9+
context "and no exisitng record" do
10+
11+
it "creates a new record" do
12+
expect{
13+
Easy::ReferenceData.refresh User, :system_code, 1, name: "Jane", email: "jane@example.com"
14+
}.to change{ User.count }
15+
end
16+
17+
end
18+
19+
context "and an exisitng record" do
20+
it "updates the existing record" do
21+
user = User.create(system_code: 1, name: "Jo")
22+
23+
expect{ Easy::ReferenceData.refresh User, :system_code, 1, name: "Jane", email: "jane@example.com" }.to change{ user.reload.name }.to "Jane"
24+
end
25+
26+
it "does not create duplicate records" do
27+
user = User.create(system_code: 1, name: "Jo")
28+
29+
expect{ Easy::ReferenceData.refresh User, :system_code, 1, name: "Jo", email: "jane@example.com" }.not_to change{ User.count }
30+
end
31+
end
32+
end
33+
34+
end
35+
end

spec/spec_helper.rb

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
require 'active_record'
2+
require 'database_cleaner'
3+
4+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
5+
6+
require 'support/models'
7+
8+
# This file was generated by the `rspec --init` command. Conventionally, all
9+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
10+
# The generated `.rspec` file contains `--require spec_helper` which will cause
11+
# this file to always be loaded, without a need to explicitly require it in any
12+
# files.
13+
#
14+
# Given that it is always loaded, you are encouraged to keep this file as
15+
# light-weight as possible. Requiring heavyweight dependencies from this file
16+
# will add to the boot time of your test suite on EVERY test run, even for an
17+
# individual file that may not need all of that loaded. Instead, consider making
18+
# a separate helper file that requires the additional dependencies and performs
19+
# the additional setup, and require it from the spec files that actually need
20+
# it.
21+
#
22+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23+
RSpec.configure do |config|
24+
# rspec-expectations config goes here. You can use an alternate
25+
# assertion/expectation library such as wrong or the stdlib/minitest
26+
# assertions if you prefer.
27+
config.expect_with :rspec do |expectations|
28+
# This option will default to `true` in RSpec 4. It makes the `description`
29+
# and `failure_message` of custom matchers include text for helper methods
30+
# defined using `chain`, e.g.:
31+
# be_bigger_than(2).and_smaller_than(4).description
32+
# # => "be bigger than 2 and smaller than 4"
33+
# ...rather than:
34+
# # => "be bigger than 2"
35+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36+
end
37+
38+
# rspec-mocks config goes here. You can use an alternate test double
39+
# library (such as bogus or mocha) by changing the `mock_with` option here.
40+
config.mock_with :rspec do |mocks|
41+
# Prevents you from mocking or stubbing a method that does not exist on
42+
# a real object. This is generally recommended, and will default to
43+
# `true` in RSpec 4.
44+
mocks.verify_partial_doubles = true
45+
end
46+
47+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
48+
# have no way to turn it off -- the option exists only for backwards
49+
# compatibility in RSpec 3). It causes shared context metadata to be
50+
# inherited by the metadata hash of host groups and examples, rather than
51+
# triggering implicit auto-inclusion in groups with matching metadata.
52+
config.shared_context_metadata_behavior = :apply_to_host_groups
53+
54+
# The settings below are suggested to provide a good initial experience
55+
# with RSpec, but feel free to customize to your heart's content.
56+
=begin
57+
# This allows you to limit a spec run to individual examples or groups
58+
# you care about by tagging them with `:focus` metadata. When nothing
59+
# is tagged with `:focus`, all examples get run. RSpec also provides
60+
# aliases for `it`, `describe`, and `context` that include `:focus`
61+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
62+
config.filter_run_when_matching :focus
63+
64+
# Allows RSpec to persist some state between runs in order to support
65+
# the `--only-failures` and `--next-failure` CLI options. We recommend
66+
# you configure your source control system to ignore this file.
67+
config.example_status_persistence_file_path = "spec/examples.txt"
68+
69+
# Limits the available syntax to the non-monkey patched syntax that is
70+
# recommended. For more details, see:
71+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
72+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
73+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
74+
config.disable_monkey_patching!
75+
76+
# This setting enables warnings. It's recommended, but in some cases may
77+
# be too noisy due to issues in dependencies.
78+
config.warnings = true
79+
80+
# Many RSpec users commonly either run the entire suite or an individual
81+
# file, and it's useful to allow more verbose output when running an
82+
# individual spec file.
83+
if config.files_to_run.one?
84+
# Use the documentation formatter for detailed output,
85+
# unless a formatter has already been configured
86+
# (e.g. via a command-line flag).
87+
config.default_formatter = "doc"
88+
end
89+
90+
# Print the 10 slowest examples and example groups at the
91+
# end of the spec run, to help surface which specs are running
92+
# particularly slow.
93+
config.profile_examples = 10
94+
95+
# Run specs in random order to surface order dependencies. If you find an
96+
# order dependency and want to debug it, you can fix the order by providing
97+
# the seed, which is printed after each run.
98+
# --seed 1234
99+
config.order = :random
100+
101+
# Seed global randomization in this process using the `--seed` CLI option.
102+
# Setting this allows you to use `--seed` to deterministically reproduce
103+
# test failures related to randomization by passing the same `--seed` value
104+
# as the one that triggered the failure.
105+
Kernel.srand config.seed
106+
=end
107+
108+
config.before(:suite) do
109+
DatabaseCleaner.strategy = :transaction
110+
DatabaseCleaner.clean_with(:transaction)
111+
end
112+
113+
config.before(:each) do
114+
DatabaseCleaner.start
115+
end
116+
117+
config.after(:each) do
118+
DatabaseCleaner.clean
119+
end
120+
121+
end

spec/support/models.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'active_record'
2+
load 'support/schema.rb'
3+
4+
class User < ActiveRecord::Base
5+
end

spec/support/schema.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'active_record'
2+
3+
ActiveRecord::Schema.define do
4+
self.verbose = false
5+
6+
create_table :users, force: true do |t|
7+
t.integer :system_code
8+
t.string :name
9+
t.string :email
10+
end
11+
end

0 commit comments

Comments
 (0)