Giter Club home page Giter Club logo

accept_values_for's Introduction

Accept values for

Description

In order to spec ActiveRecord models. I decided to write a few custom matchers that makes the work match easier:

Matchers

  • accept_values_for
  • discover

Accept values for

RSpec matcher to test the validation

describe User do

  subject { User.new(@valid_attributes)}
  
  it { should accept_values_for(:email, "[email protected]", "[email protected]") }
  it { should_not accept_values_for(:email, "invalid", nil, "a@b", "[email protected]") }
end

Discovery matcher

RSpec matcher to test named scopes

describe "#by_category_id named scope" do
  let(:given_category) do 
    Factory.create(:given_category)
  end


  let(:product_in_given_category) do
    Factory.create(
      :product,
      :categories => [given_category]
    )
  end

  let(:product_not_in_given_category) do
    Factory.create(
      :product,
      :categories => [Factory.create(:category)]
    )
  end

  # This might be tricky to redefine subject as the finder result
  # but in this way we can delegate the matcher to subject and 
  # avoid writing test descriptions.
  subject { described_class.by_category_id(given_category.id) }

  it { should discover(product_in_given_category) }
  it { should_not discover(product_not_in_given_category) }

end 

Dependencies

  • ActiveRecord
  • Rspec

Install

Gemfile

gem 'accept_values_for'

spec_helper.rb:

require 'accept_values_for'
require 'discover'

Self-Promotion

Like accept_values_for?

Follow the repository on GitHub.

Read author blog.

accept_values_for's People

Contributors

bogdan avatar deepj avatar masterkain avatar tylergannon avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.