You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 12, 2023. It is now read-only.
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/validation_examples_matcher`. To experiment with that code, run `bin/console` for an interactive prompt.
TODO: Delete this and the text above, and describe your gem
7
+
e.g.
8
+
9
+
```ruby
10
+
RSpec.describe MyModeldo
11
+
...
12
+
13
+
it { is_expected.to be_invalid_on(:name).with(nil) }
14
+
it { is_expected.to be_invalid_on(:name).with('') }
15
+
it { is_expected.to be_valid_on(:name).with('my name') }
16
+
end
17
+
```
18
+
19
+
ValidationExamplesMatcher sets a particular value ― nil, empty string and 'my name' ― actually.
20
+
Then the matcher calls `valid?` or `invalid?` method and checks results of validation.
8
21
9
22
## Installation
10
23
@@ -18,26 +31,67 @@ And then execute:
18
31
19
32
$ bundle
20
33
21
-
Or install it yourself as:
34
+
## Usage
22
35
23
-
$ gem install validation_examples_matcher
36
+
Let me show how to use ValidationExamplesMatcher in case of following model.
24
37
25
-
## Usage
38
+
```ruby
39
+
classMyModel < ActiveRecord::Base
40
+
validates :name, presence:true
41
+
validates :name, length: { maxmum:4 }, on::create
42
+
end
43
+
```
44
+
45
+
First, you have to define `subject` as target model object.
46
+
It's good to define it as *valid* object.
47
+
Creating object with [factory_girl](https://github.com/thoughtbot/factory_girl) is very nice.
48
+
49
+
```ruby
50
+
RSpec.define MyModeldo
51
+
subject { FactoryGirl.build(:my_model) }
52
+
# subject { MyModel.new } => also ok but not recommended
53
+
end
54
+
```
55
+
56
+
Second, write *invalid case examples* and *valid case examples* using ValidationExamplesMatcher.
57
+
In case of `MyModel`, `:name` attribute is invalid when its' value is nil or empty string.
58
+
`'my name'` is typical valid example.
26
59
27
-
TODO: Write usage instructions here
60
+
```ruby
61
+
RSpec.define MyModeldo
62
+
subject { FactoryGirl.build(:my_model) }
28
63
29
-
## Development
64
+
it { is_expected.to be_invalid_on(:name).with(nil) }
65
+
it { is_expected.to be_invalid_on(:name).with('') }
66
+
it { is_expected.to be_valid_on(:name).with('my name') }
67
+
end
68
+
```
30
69
31
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
Arguments of `be_invalid_on` or `be_valid_on` indicate attribute name.
71
+
And arguments of `with` chain is a value which will be set to the attribute.
32
72
33
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
`ActiveModel::Validations` can define validations on specific contexts.
74
+
`MyModel` has `length` validation only on `:create` context.
75
+
You can also define validation spec for such case using `on_context` chain.
76
+
77
+
```ruby
78
+
RSpec.define MyModeldo
79
+
subject { FactoryGirl.build(:my_model) }
80
+
81
+
it { is_expected.to be_invalid_on(:name).with(nil) }
82
+
it { is_expected.to be_invalid_on(:name).with('') }
83
+
it { is_expected.to be_valid_on(:name).with('my name') }
84
+
85
+
it { is_expected.to be_valid_on(:name).with('4cha').on_context(:create) }
86
+
it { is_expected.to be_invalid_on(:name).with('5char').on_context(:create) }
87
+
end
88
+
```
34
89
35
90
## Contributing
36
91
37
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validation_examples_matcher.
92
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nisshiee/validation_examples_matcher.
38
93
39
94
40
95
## License
41
96
42
97
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
0 commit comments