GithubHelp home page GithubHelp logo

willcannings / rspec-describe-method Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brett-richardson/rspec-describe-method

0.0 2.0 0.0 100 KB

Shorthand helper for tersely describing RSpec subject methods without a whole lot of code!

Home Page: http://brett-richardson.github.io/rspec-describe-method

License: MIT License

Ruby 100.00%

rspec-describe-method's Introduction

Build Status

Using Rspec DescribeMethod

Using Rspec DescribeMethod is super simple.

In your Gemfile:

group :development, :testing do
  gem 'rspec-describe-method'
end

In your spec_helper.rb:

require 'rspec/describe_method'

And your specs can look like this:

describe String do
    describe_method '.new' do
        it{ should be_a String }
    end

    describe_method '#concat', 'argument' do
        it{ should match /argument/i }
    end

    context 'with an instance' do
        subject{ String.new 'test' }

        describe_method '#upcase' do
            it{ should eq 'TEST' }
        end
    end
end

So what's happening behind the scenes?

Essentially we do a little bit of meta-programming and replace the describe_method with a normal describe and subject call as so:

describe User do
    #--- This... ---------------

    describe_method '#to_s' do
        it{ should be_a String }
    end

    #--- Is Equivalent to... ---

    describe '#to_s' do
        subject{ described_class.new.to_s }

        it{ should be_a String }
    end
end
describe User do
    subject( :instance ){ described_class.new }

    #--- This... ----------------

    describe_method '.find', 1 do
        it{ should be_a User }
    end

    #--- Is Equivalent to... ----

    describe '.find( 1 )' do
        subject{ instance.class.find 1 }

        it{ should be_a String )
    end
end

Describe a method call on the current test subject in your specs with 'describe_method', and a # for instance methods and a . for class methods.

Instances will automatically delagate to their class, and classes will automatically create an instance of themselves.

Alias method: when_calling

The alias method when_calling is provided which makes specs more human-readable, especially when calls are nested within eachother:

describe String do
    subject{ String.new 'test' }

    when_calling '#upcase' {
        it{ should eq 'TEST' }

        when_calling '#+', 'ING' {
            it{ should eq 'TESTING' }
        }
    }
end

NOTE

When the subject is a Class, and you call an instance method describe_method "#something" the class will call .new on itself with NO ARGUMENTS, you may not want this behavior.

rspec-describe-method's People

Contributors

brett-richardson avatar willcannings 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.