[rspec-users] How can I spec protect method

Matt Wynne matt at mattwynne.net
Wed Oct 21 13:38:59 EDT 2009


On 21 Oct 2009, at 17:31, Elza wrote:

>
> How can I spec the 3rd line of my controller?

What does that method do to the other objects in your domain? Can you  
describe that behaviour easily using mocks and stubs?

If not, you would be best to move the method out of the controller and  
onto another class, such as the User model. This is called 'listening  
to your tests'[1]

[1]http://www.mockobjects.com/book/listening-to-the-tests.html

> update_successful, error_msg = update_channels_association
>
>  def create
>    @user = User.new(get_param(:user, :hash))
>
>    if @user.save
>      update_successful, error_msg = update_channels_association
>      set_flash_if update_successful,
> :resource_successfully_created.t(:resource => :user.t[:one]),  
> error_msg
>
>      redirect_to @user
>    else
>      render :action => :new
>    end
>  end
> **************************
> My spec
>
> describe UsersController do
>  describe "POST create" do
>    ##############################################################
>    should_require_login :post, :create
>
>    ##############################################################
>    describe "authenticated user" do
>      ##############################################################
>      before(:each) do
>        login_as_user
>
>        @user = mock_model(User, :save => nil)
>        User.stub!(:new).and_return(@user)
>        @params = {'name' => 'Mary'}
>      end
>
>      ##############################################################
>      def do_post
>        post :create
>      end
>
>      ##############################################################
>      it "should build a new user" do
>        User.should_receive(:new).with(@params).and_return(@user)
>        post :create, :user => @params
>      end
>
>      ##############################################################
>      it "should save the user" do
>        @user.should_receive(:save)
>        do_post
>      end
>
>      ##############################################################
>      context "when the user saves successfully" do
>        before(:each) do
>          @user.stub!(:save).and_return true
>          controller.should_receive(:update_channels_association)
>        end
>
>
>        it "should set a flash[:notice] user" do
>          do_post
>          flash[:notice].should ==
> :resource_successfully_created.t(:resource => :user.t[:one])
>        end
> *************************
>
> The error
> 'UsersController POST create authenticated user when the user saves
> successfully should set a flash[:notice] user' FAILED
> expected: "Usu�rio criado(a) com sucesso.",
>     got: nil (using ==)
>
> I know it is missing to verify the local variable update_successful.
>
> If anyone can help me.
> Cheers
> EM
> -- 
> View this message in context: http://www.nabble.com/How-can-I-spec-protect-method-tp25995877p25995877.html
> Sent from the rspec-users mailing list archive at Nabble.com.
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://mattwynne.net
+447974 430184



More information about the rspec-users mailing list