GithubHelp home page GithubHelp logo

cliches's Introduction

Cliches: Rid your tests of boring, repetitive hashes.

What is this?

Cliches solves the endless problem of having to pass large hashes of parameters to different methods in your tests. This is a problem that constantly appears when writing tests, a problem that people either solve at the wrong level or don't solve at all.

Installation

Add this line to your application's Gemfile:

gem 'cliches'

And then run:

$ bundle

Why should I use this?

You may be thinking that fixtures of factories already solve this problem. Well, they do, but only partly. Both of these solutions will couple your attributes with ActiveRecord models, which really isn't what you want. Consider these scenarios:

  1. You are writing an integration test. How can you easily get the attributes from a factory/fixture and just use them to do a POST?
  2. You want to use the attributes from one factory to update an existing record. How do you do that without instantiating a record and potentially getting a lot of attributes you're not interested in.
  3. You have a method that takes a hash but has nothing to do with ActiveRecord. You will be calling this method with very similar arguments from many different tests. How do you keep these similar attributes organized?

OK. What does this look like?

cliches are defined in your test directory, in plain Ruby files that look like this:

Cliches.define(:customer).as(
  first_name: "Alice",
  last_name: "Wilson",
  state: "AL",
  country: "US"
)

That's it; easy-peasy. Their basic usage looks like this:

Customer.create!(cliches(:customer))

Or like this to just instantiate:

Customer.new(cliches(:customer))

But you can also do this:

customer.update_attributes(cliches(:customer))

And even this:

post :create, {customer: cliches(:customer)}

Rails Goodies

There are a couple of things that the cliches gems provide to Rails developers to keep their syntax sugar-levels adequately high.

There is a shortcut for building models:

# This looks up cliches(:customer) and instantiates a model.
Customer.cliche
 
# This looks up cliches(:returning_customer) and instantiates a model
Customer.cliche(:returning)

# This does the same, but also saves the model
Customer.cliche!(:returning)

The Rails integration also comes with a simple little generator that you can use to create cliche files. Here's an example of how to run it:

$ bin/rails g cliches:new customer

This will generate a file called test/cliches/customer.rb and populate it with a simple example of how to use cliche.

Contributing

  1. Fork it ( https://github.com/thegengen/cliches/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

cliches's People

Watchers

 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.