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

Commit 398ae5d

Browse files
committed
Merge pull request #6 from nisshiee/readme
Write README
2 parents 1143424 + 2090ad8 commit 398ae5d

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

README.md

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@
22

33
[![Build Status](https://travis-ci.org/nisshiee/validation_examples_matcher.svg?branch=master)](https://travis-ci.org/nisshiee/validation_examples_matcher)
44

5-
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.
5+
ValidationExamplesMatcher supports writing rails model's validation specs.
66

7-
TODO: Delete this and the text above, and describe your gem
7+
e.g.
8+
9+
```ruby
10+
RSpec.describe MyModel do
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.
821

922
## Installation
1023

@@ -18,26 +31,67 @@ And then execute:
1831

1932
$ bundle
2033

21-
Or install it yourself as:
34+
## Usage
2235

23-
$ gem install validation_examples_matcher
36+
Let me show how to use ValidationExamplesMatcher in case of following model.
2437

25-
## Usage
38+
```ruby
39+
class MyModel < 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 MyModel do
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.
2659

27-
TODO: Write usage instructions here
60+
```ruby
61+
RSpec.define MyModel do
62+
subject { FactoryGirl.build(:my_model) }
2863

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+
```
3069

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.
3272

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 MyModel do
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+
```
3489

3590
## Contributing
3691

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.
3893

3994

4095
## License
4196

4297
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43-

0 commit comments

Comments
 (0)