[rspec-devel] [ rspec-Feature Requests-11707 ] paramters for shared behaviours

noreply at rubyforge.org noreply at rubyforge.org
Mon Nov 19 00:03:14 EST 2007


Feature Requests item #11707, was opened at 2007-06-20 06:19
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=11707&group_id=797

Category: None
Group: None
Status: Open
Priority: 3
Submitted By: David Green (david_green)
Assigned to: Nobody (None)
Summary: paramters for shared behaviours

Initial Comment:
It would be useful if shared behaviours could accept parameters.

this thread on the rspec users mailing list is a good example of when such a feature is needed:

http://www.nabble.com/dynamic-specs-tf3927280.html

----------------------------------------------------------------------

Comment By: Chad Humphries (spicycode)
Date: 2007-11-19 00:03

Message:
Moved to http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/96-11707-paramters-for-shared-behaviours#ticket-96-2

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-06-20 07:35

Message:
Giving this some more thought, this turns out to be rather problematic. Not only due to the re-design work but because of usage problems. Let's say you have the following setup:

describe "All Accounts", :shared => true do |account|
  it "should let you withdraw less than it has" do
    account.deposit 50
    account.withdraw 20
    account.balance.should == 30
  end
  it "should not let you withdraw more than it has" do
    account.deposit 20
    account.withdraw 21
    account.balance.should == 20
  end
  ...
end

describe CheckingAccount do
  it_should_behave_like "All Accounts", CheckingAccount.new
  ...
end

This seems perfectly reasonable, right? Well the first problem is that the two examples in the shared behaviour don't have access to the account instance. You'd have to do this to your implementation:

describe "All Accounts", :shared => true do |acc|
  def account
    acc
  end
  ...
end

Once that problem is solved, we would find that the second example would fail saying the balance is 29. Why? Because CheckingAccount.new would only be evaluated once, and the same instance would be used in both of the examples.

We could solve by passing a block to it_should_behave_like:

describe CheckingAccount do
  it_should_behave_like "All Accounts" {CheckingAccount.new}
  ...
end

Or passing method names:

describe CheckingAccount do
  it_should_behave_like "All Accounts", :new_checking_account
  
  def new_checking_account
    CheckingAccount.new
  end
  ...
end

But now we're starting to loose the benefits to verbosity and confusion.

Anybody see a simpler way?


----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=11707&group_id=797


More information about the rspec-devel mailing list