From 03ef9820197a872620875eb80efac8c749317e2b Mon Sep 17 00:00:00 2001 From: Hirokazu Nishioka Date: Tue, 26 Jan 2016 22:56:17 +0900 Subject: [PATCH 1/2] Write README --- README.md | 75 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2efb1b1..e0df881 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,22 @@ [![Build Status](https://travis-ci.org/nisshiee/validation_examples_matcher.svg?branch=master)](https://travis-ci.org/nisshiee/validation_examples_matcher) -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. +ValidationExamplesMatcher supports writing rails model's validation specs. -TODO: Delete this and the text above, and describe your gem +e.g. + +```ruby +RSpec.describe MyModel do + ... + + it { is_expected.to be_invalid_on(:name).with(nil) } + it { is_expected.to be_invalid_on(:name).with('') } + it { is_expected.to be_valid_on(:name).with('my name') } +end +``` + +ValidationExamplesMatcher sets a particular value ― nil, empty string and 'my name' ― actually. +Then the matcher calls `valid?` or `invalid?` method and checks results of validation. ## Installation @@ -18,26 +31,66 @@ And then execute: $ bundle -Or install it yourself as: +## Usage - $ gem install validation_examples_matcher +Let me show how to use ValidationExamplesMatcher in case of following model. -## Usage +```ruby +class MyModel < ActiveRecord::Base + validates :name, presence: true + validates :name, length: { maxmum: 4 }, on: :create +end +``` + +First, you have to define `subject` as target model object. +It's good to define it as *valid* object. +Creating object with [factory_girl](https://github.com/thoughtbot/factory_girl) is very nice. + +```ruby +RSpec.define MyModel do + subject { FactoryGirl.build(:my_model) } + # subject { MyModel.new } => also ok but not recommended +end +``` + +Second, write *invalid case examples* and *valid case examples* using ValidationExamplesMatcher. +In case of `MyModel`, `:name` attribute is invalid when its' value is nil or empty string. +`'my name'` is typical valid example. -TODO: Write usage instructions here +```ruby +RSpec.define MyModel do + subject { FactoryGirl.build(:my_model) } -## Development + it { is_expected.to be_invalid_on(:name).with(nil) } + it { is_expected.to be_invalid_on(:name).with('') } + it { is_expected.to be_valid_on(:name).with('my name') } +end +``` -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. +Arguments of `be_invalid_on` or `be_valid_on` indicate attribute name. +And arguments of `with` chain is a value which will be set to the attribute. -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). +`ActiveModel::Validations` can define validations on specific contexts. +`MyModel` has `length` validation only on `:create` context. +You can also define validation spec for such case using `on_context` chain. + +```ruby +RSpec.define MyModel do + subject { FactoryGirl.build(:my_model) } + + it { is_expected.to be_invalid_on(:name).with(nil) } + it { is_expected.to be_invalid_on(:name).with('') } + it { is_expected.to be_valid_on(:name).with('my name') } + + it { is_expected.to be_invalid_on(:name).with('5char').on_context(:create) } +end +``` ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validation_examples_matcher. +Bug reports and pull requests are welcome on GitHub at https://github.com/nisshiee/validation_examples_matcher. ## License The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). - From 2090ad84cf4527b46b70f193c63a542a25bd39bd Mon Sep 17 00:00:00 2001 From: Hirokazu Nishioka Date: Tue, 26 Jan 2016 22:58:07 +0900 Subject: [PATCH 2/2] Add example on README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e0df881..e5ad2d9 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ RSpec.define MyModel do it { is_expected.to be_invalid_on(:name).with('') } it { is_expected.to be_valid_on(:name).with('my name') } + it { is_expected.to be_valid_on(:name).with('4cha').on_context(:create) } it { is_expected.to be_invalid_on(:name).with('5char').on_context(:create) } end ```