method
stub_chain
1.2.8 -
Show latest stable
-
2 notes -
Class: Spec::Mocks::Methods
- 1.1.4
- 1.1.12
- 1.2.0
- 1.2.8 (0)
- 1.3.0 (38)
- 1.3.1 (0)
- What's this?
Register or
log in
to add new notes.
hosiawak -
March 4, 2010 - (>= 1.2.8)
plugawy -
July 21, 2010
5 thanks
stub_chain is very useful when testing controller code
or any other chained method call type that you’d like to stub, example:
in your controller:
def new @user = current_site.users.new end
in your spec:
it "#new should assign a @user" do u = mock("User") controller.stub_chain(:current_site, :users, :new).and_return(u) assigns[:user].should == u end
whereas before you had to stub each chained method call separately:
it "#new should assign a @user" do u = mock("User") users = mock("Users collection", :new => u) site = mock("Site", :users => users) controller.stub!(:current_site).and_return(site) assigns[:user].should == u end
Please note that stub_chain was added to RSpec in version 1.2.6
1 thank
Works only inside the "it" block
Please note that stub_chain doesn’t work outside of the it...do...end block.
If you need to create more complicated chains using a function you need to use the old way.


