[rspec-users] Preconditions

Pat Maddox pergesu at gmail.com
Fri Sep 7 16:59:52 EDT 2007

On 9/7/07, Geoffrey Wiseman <geoffrey.wiseman at gmail.com> wrote:
> Sorry, lots of questions these days.
> Is there a normal approach for preconditions?  In JUnit, I might put a few
> assertions in the setUp() just to make sure that the test 'data' I've
> created meets expectations before going to test it.
> So, for instance, I've got an object that is audited using acts_as_audited
> and I'd like to test the XML that results from model.revisions.to_xml().
> I've created the object and modified it twice, so I expect there to be three
> revisions, as far as the test goes.  It's not really a 'spec', because it's
> not part of the contract that model objects have three revisions, it's just
> something I want to make sure is true before I run off and conduct some
> tests.
> I could raise an exception in the before block if the precondition isn't
> met, any other choices?
>   - Geoffrey
> --
> Geoffrey Wiseman
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

I'm not sure why you say this isn't a spec.  Specs are about behavior
of the system, not contracts.  In this case you have two examples

describe MyModel, " when saved twice" do
  before(:each) do
    @model = MyModel.create :title => "foo"
    2.times { @model.save }

  it "should have three revisions" do
    @model.should have(3).revisions

  it "should render all revisions in a single XML doc" do
    Hash.from_xml(@model.to_xml).should ==
      { "model" => { "revision" => 1, "title" => "foo" },
        "model" => { "revision" => 2, "title" => "foo" },
        "model" => { "revision" => 3, "title" => "foo" } }

Also I would just have one description (like above) that verifies that
it works as you expect.  In specs for other behaviors you can then
just assume that it works.


More information about the rspec-users mailing list