Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit f6c96dc

Browse files
committed
Merge pull request #2 from nisshiee/specs
Add examples spec
2 parents e6da122 + 06f053b commit f6c96dc

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

spec/examples/validations_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'spec_helper'
2+
3+
class TestModel
4+
include ActiveModel::Model
5+
6+
attr_accessor :attr
7+
validates :attr, presence: true
8+
validates :attr, length: { maximum: 4 }, on: :a_context
9+
end
10+
11+
RSpec.describe TestModel do
12+
subject { TestModel.new }
13+
14+
context 'when target attribute has valid value' do
15+
it { is_expected.to be_valid_on(:attr).with('valid value') }
16+
it { is_expected.not_to be_invalid_on(:attr).with('valid value') }
17+
end
18+
19+
context 'when target attribute has INVALID value' do
20+
it { is_expected.to be_invalid_on(:attr).with(nil) }
21+
it { is_expected.not_to be_valid_on(:attr).with(nil) }
22+
end
23+
24+
context 'when target attribute is validated on a certain context' do
25+
context 'when target attribute has valid value' do
26+
it { is_expected.to be_valid_on(:attr).with('1234').on_context(:a_context) }
27+
it { is_expected.not_to be_invalid_on(:attr).with('1234').on_context(:a_context) }
28+
end
29+
30+
context 'when target attribute has INVALID value' do
31+
it { is_expected.to be_invalid_on(:attr).with('12345').on_context(:a_context) }
32+
it { is_expected.not_to be_valid_on(:attr).with('12345').on_context(:a_context) }
33+
end
34+
end
35+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
22
require 'validation_examples_matcher'
3+
require 'active_model'

spec/validation_examples_matcher_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@
44
it 'has a version number' do
55
expect(ValidationExamplesMatcher::VERSION).not_to be nil
66
end
7-
8-
it 'does something useful' do
9-
expect(false).to eq(true)
10-
end
117
end

validation_examples_matcher.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "bundler", "~> 1.11"
2626
spec.add_development_dependency "rake", "~> 10.0"
2727
spec.add_development_dependency "rspec", "~> 3.0"
28+
spec.add_development_dependency "activemodel", "~> 4.2.5"
2829
end

0 commit comments

Comments
 (0)