RSpec Subjects

by woody2shoes on May 5, 2010


RSpec provides an extremely concise way of representing simple tests to be called on new instances of a class or on explicitly defined receiver objects. You can do this by using ‘subjects’ either as defined by the ‘describe’ or the ‘subject’ methods.
Download 38 MB
Download (iphone & ipod) 18.2 MB

  • Tim

    That’s useful, thank!

  • Tim

    That's useful, thank!

  • http://twitter.com/irrationalidiot Mark Turner

    I just discovered this last night while working through The Traveling Hacker from http://rubyproblems.com. Nice screencast!

  • http://twitter.com/irrationalidiot Mark Turner

    I just discovered this last night while working through The Traveling Hacker from http://rubyproblems.com. Nice screencast!

  • Anonymous

    how does the ‘subject’ method extend into say Rails specs, doesn’t it end up behaving like before (:all)?

    • http://teachmetocode.com Charles Max Wood

      Yes, it pretty much acts as a before(:all) { @subject = MyObject.new(options) }

      The subsequent examples are calls on @subject.

  • Stuart Corbishley

    how does the 'subject' method extend into say Rails specs, doesn't it end up behaving like before (:all)?

  • woody2shoes

    Yes, it pretty much acts as a before(:all) { @subject = MyObject.new(options) }

    The subsequent examples are calls on @subject.

  • James

    I like this idea, its really readable and looks more like a specification (the final version). How would I achieve the same effect for a method that returns a number.

    For example the truck must have 4 wheels?

  • James

    Ignore my last comment

    http://pastebin.com/ZLa0YtLt

  • James

    I like this idea, its really readable and looks more like a specification (the final version). How would I achieve the same effect for a method that returns a number.

    For example the truck must have 4 wheels?

  • James

    Ignore my last comment

    http://pastebin.com/ZLa0YtLt

Previous post:

Next post: