Fischer, Daniel
2007-Aug-08 21:08 UTC
[rspec-users] Can''t seem to spec a ActiveRecord::RecordInvalid exception properly...
1 def create 2 @user = User.new(params[:user]) 3 @user.save! 4 self.current_user = @user 5 redirect_to user_path(@user) 6 flash[:notice] = "Thanks for signing up!" 7 rescue ActiveRecord::RecordInvalid 8 render :action => ''new'' 9 end I can''t seem to properly spec this out. I am trying numerous things, the latest one is this, which makes sense but it still fails... it "should re-render new on an invalid record exception" do post :create, :user => {:login => nil} response.should render_template(:new) end should re-render new on an invalid record exception expected "new", got nil Any help would be great, thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070808/9cb7bada/attachment.html
Lance Carlson
2007-Aug-08 21:14 UTC
[rspec-users] Can''t seem to spec a ActiveRecord::RecordInvalid exception properly...
Does it redirect instead? On 8/8/07, Fischer, Daniel <daniel at danielfischer.com> wrote:> 1 def create > 2 @user = User.new(params[:user]) > 3 @user.save! > 4 self.current_user = @user > 5 redirect_to user_path(@user) > 6 flash[:notice] = "Thanks for signing up!" > 7 rescue ActiveRecord::RecordInvalid > 8 render :action => ''new'' > 9 end > > I can''t seem to properly spec this out. I am trying numerous things, the > latest one is this, which makes sense but it still fails... > > > it "should re-render new on an invalid record exception" do > post :create, :user => {:login => nil} > response.should render_template(:new) > end > > should re-render new on an invalid record exception > expected "new", got nil > > Any help would be great, thanks! > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Fischer, Daniel
2007-Aug-08 21:22 UTC
[rspec-users] Can''t seem to spec a ActiveRecord::RecordInvalid exception properly...
It''s redirecting to /users/1 so it''s not causing a fail on the exception, which I don''t know why. My question is how to properly do this in rSpec? On 8/8/07, Lance Carlson <lancecarlson at gmail.com> wrote:> > Does it redirect instead? > > On 8/8/07, Fischer, Daniel <daniel at danielfischer.com> wrote: > > 1 def create > > 2 @user = User.new(params[:user]) > > 3 @user.save! > > 4 self.current_user = @user > > 5 redirect_to user_path(@user) > > 6 flash[:notice] = "Thanks for signing up!" > > 7 rescue ActiveRecord::RecordInvalid > > 8 render :action => ''new'' > > 9 end > > > > I can''t seem to properly spec this out. I am trying numerous things, the > > latest one is this, which makes sense but it still fails... > > > > > > it "should re-render new on an invalid record exception" do > > post :create, :user => {:login => nil} > > response.should render_template(:new) > > end > > > > should re-render new on an invalid record exception > > expected "new", got nil > > > > Any help would be great, thanks! > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070808/5817f2a6/attachment.html
Lance Carlson
2007-Aug-08 21:35 UTC
[rspec-users] Can''t seem to spec a ActiveRecord::RecordInvalid exception properly...
Is it redirecting or is it rendering new? On 8/8/07, Fischer, Daniel <daniel at danielfischer.com> wrote:> It''s redirecting to /users/1 so it''s not causing a fail on the exception, > which I don''t know why. My question is how to properly do this in rSpec? > > > On 8/8/07, Lance Carlson <lancecarlson at gmail.com> wrote: > > Does it redirect instead? > > > > On 8/8/07, Fischer, Daniel <daniel at danielfischer.com> wrote: > > > 1 def create > > > 2 @user = User.new(params[:user]) > > > 3 @ user.save! > > > 4 self.current_user = @user > > > 5 redirect_to user_path(@user) > > > 6 flash[:notice] = "Thanks for signing up!" > > > 7 rescue ActiveRecord::RecordInvalid > > > 8 render :action => ''new'' > > > 9 end > > > > > > I can''t seem to properly spec this out. I am trying numerous things, the > > > latest one is this, which makes sense but it still fails... > > > > > > > > > it "should re-render new on an invalid record exception" do > > > post :create, :user => {:login => nil} > > > response.should render_template(:new) > > > end > > > > > > should re-render new on an invalid record exception > > > expected "new", got nil > > > > > > Any help would be great, thanks! > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2007-Aug-08 22:10 UTC
[rspec-users] Can''t seem to spec a ActiveRecord::RecordInvalid exception properly...
On 8/8/07, Fischer, Daniel <daniel at danielfischer.com> wrote:> 1 def create > 2 @user = User.new(params[:user]) > 3 @user.save! > 4 self.current_user = @user > 5 redirect_to user_path(@user) > 6 flash[:notice] = "Thanks for signing up!" > 7 rescue ActiveRecord::RecordInvalid > 8 render :action => ''new'' > 9 endTry this: describe "/users/create" do before(:each) do User.stub!(:new).and_return(@user = mock_model(User)) end it "should redirect to show on successful save" do @user.should_receive(:save!) post :create response.should redirect_to(user_path(@user)) end it "should re-render new on failed save" do @user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(@user)) post :create response.should render_template(''new'') end end Cheers, David> > I can''t seem to properly spec this out. I am trying numerous things, the > latest one is this, which makes sense but it still fails... > > > it "should re-render new on an invalid record exception" do > post :create, :user => {:login => nil} > response.should render_template(:new) > end > > should re-render new on an invalid record exception > expected "new", got nil > > Any help would be great, thanks! > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Eivind Uggedal
2007-Aug-10 20:04 UTC
[rspec-users] Can''t seem to spec a ActiveRecord::RecordInvalid exception properly...
I asked exactly this question on this list 2 days ago... See http://pastie.caboo.se/85887 for a working example. One has to mock out ActiveRecord::Errors::full_messages for it to work under Rails. Cheers, Eivind Uggedal On 8/9/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 8/8/07, Fischer, Daniel <daniel at danielfischer.com> wrote: > > 1 def create > > 2 @user = User.new(params[:user]) > > 3 @user.save! > > 4 self.current_user = @user > > 5 redirect_to user_path(@user) > > 6 flash[:notice] = "Thanks for signing up!" > > 7 rescue ActiveRecord::RecordInvalid > > 8 render :action => ''new'' > > 9 end > > Try this: > > describe "/users/create" do > before(:each) do > User.stub!(:new).and_return(@user = mock_model(User)) > end > > it "should redirect to show on successful save" do > @user.should_receive(:save!) > post :create > response.should redirect_to(user_path(@user)) > end > > it "should re-render new on failed save" do > @user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(@user)) > post :create > response.should render_template(''new'') > end > end > > Cheers, > David > > > > > I can''t seem to properly spec this out. I am trying numerous things, the > > latest one is this, which makes sense but it still fails... > > > > > > it "should re-render new on an invalid record exception" do > > post :create, :user => {:login => nil} > > response.should render_template(:new) > > end > > > > should re-render new on an invalid record exception > > expected "new", got nil > > > > Any help would be great, thanks! > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >