Giter VIP home page Giter VIP logo

rspec-activemodel-mocks's Introduction

RSpec::ActiveModel::Mocks Build Status

RSpec::ActiveModel::Mocks provides tools for testing ActiveModel classes.

mock_model(Person, name: "Fred")

Install

Add this line to your application's gemfile:

gem 'rspec-activemodel-mocks'

And then execute:

$ bundle

If you are using rspec-rails and have followed the installation instructions there, you're all set to use stub_model and mock_model.

To use stub_model and mock_model without rspec-rails, require the following file:

require 'rspec/active_model/mocks'

Usage

Mock

Creates a test double representing string_or_model_class with common ActiveModel methods stubbed out. Additional methods may be easily stubbed (via add_stubs) if stubs is passed. This is most useful for impersonating models that don't exist yet.

ActiveModel methods, plus new_record?, are stubbed out implicitly. new_record? returns the inverse of persisted?, and is present only for compatibility with extension frameworks that have yet to update themselves to the ActiveModel API (which declares persisted?, not new_record?).

string_or_model_class can be any of:

  • A String representing a Class that does not exist
  • A String representing a Class that extends ActiveModel::Naming
  • A Class that extends ActiveModel::Naming

Stub

Creates an instance of Model with to_param stubbed using a generated value that is unique to each object. If Model is an ActiveRecord model, it is prohibited from accessing the database.

For each key in stubs, if the model has a matching attribute (determined by respond_to?) it is simply assigned the submitted values. If the model does not have a matching attribute, the key/value pair is assigned as a stub return value using RSpec's mocking/stubbing framework.

persisted? is overridden to return the result of !id.nil? This means that by default persisted? will return true. If you want the object to behave as a new record, sending it as_new_record will set the id to nil. You can also explicitly set :id => nil, in which case persisted? will return false, but using as_new_record makes the example a bit more descriptive.

While you can use stub_model in any example (model, view, controller, helper), it is especially useful in view examples, which are inherently more state-based than interaction-based.

stub_model(Person)
stub_model(Person).as_new_record
stub_model(Person, :to_param => 37)
stub_model(Person) {|person| person.first_name = "David"}

rspec-activemodel-mocks's People

Contributors

ablignaut avatar alindeman avatar benoittgt avatar bquorning avatar cupakromer avatar jonrowe avatar olleolleolle avatar orien avatar thomas-holmes 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.