On 18.4.2008, at 12.32, Andy Croll wrote:
OK I'm back and surely missing stuff again...
In my controller tests I'm checking for a redirect after a destroy
action.
First up it's a single resource ("map.resource :cart" in routes.rb)
in CartsController.rb
def destroy
@cart = Cart.find(session[:cart], :include => :items) if
session[:cart]
if @cart
Cart.delete(@cart.id)
session[:cart] = nil
end
redirect_to :back
end
in the spec: (note I'm also testing the case where the is no session
var)
describe CartsController, "empty existing cart (destroy in db)" do
before(:each) do
@item1 = mock_model(CartItem, :id => 1)
@item2 = mock_model(CartItem, :id => 2)
@cart = mock_model(Cart, :id => 1)
@cart.stub!(:items).and_return([EMAIL PROTECTED], @item2])
Cart.stub!(:find).and_return(@cart)
session[:cart] = 1000
request.env["HTTP_REFERER"] = "/prev/page"
end
def do_delete
delete :destroy
end
it "should look in the session for a cart" do
Cart.should_receive(:find).with(session[:cart], {:include =>
:items}).and_return(@cart)
do_delete
end
it "should delete the cart" do
Cart.should_receive(:delete).with(1).and_return(true)
do_delete
end
it "should delete the cart id from the session" do
do_delete
session[:cart].should == nil
end
it "should redirect to the previous page" do
response.should be_redirect
end
I would perhaps use
it "should redirect to the previous page" do response.should redirect_to "/prev/page" end
since you're also interested in where the redirect goes, but dunno if it solves your problem.
//jarkko
end Unfortunately I'm getting this. 1)'CartsController empty existing cart (destroy in db) should redirect tothe previous page' FAILED expected redirect? to return true, got false Which just ain't true! Am I missing anything? I'd also like to hear any comment on my spec 'style' as I'm new! -- Posted via http://www.ruby-forum.com/. _______________________________________________ rspec-users mailing list [email protected] http://rubyforge.org/mailman/listinfo/rspec-users
-- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ rspec-users mailing list [email protected] http://rubyforge.org/mailman/listinfo/rspec-users
