[rspec-users] Controller Isolation

David Chelimsky dchelimsky at gmail.com
Wed Nov 22 05:53:22 EST 2006


On 11/21/06, Dylan Egan <dylanegan at gmail.com> wrote:
> Hi,
>
> I have a project that I had running under rspec 0.6.x and recently upgraded
> to 0.7.2.
>
> I am trying to isolate my controllers from the database as I go through and
> change all the specs to run under 0.7.2.
>
> I am having a problem where I need to make the create! method return the
> mocked object as well as raise RecordInvalid exception. Is this at all
> possible?

AFAIK, Ruby won't let you return an object and raise an error at the
same time. You get one or the other.

> I use the rescue statement in my controller methods and not @
> object.new_record? or @object.valid? to check whether to redirect or render.
>
> -- controller --
>
>   def create
>     @star = Star.create!(params[:star])
>     redirect_to star_path(@star)
>   rescue ActiveRecord::RecordInvalid
>     render :action => 'new'
>   end
>
> -- spec --
>   @star = mock('star')
>   Star.should_receive(:create!).with({ 'name' => nil }).and_return(@star)

Hmmm. As I said above, I can't imagine that Ruby will let you do that.
What I would do in this case is this:

-- spec --
  @star = mock('star')
  Star.should_receive(:create!).with({ 'name' => nil
}).and_raise(ActiveRecord::RecordInvalid)
  Star.should_receive(:create).with({ 'name' => nil }).and_return(@star)

-- controller --

  def create
    @star = Star.create!(params[:star])
    redirect_to star_path(@star)
  rescue ActiveRecord::RecordInvalid
    @star = Star.create(params[:star])
    render :action => 'new'
  end

OR

  def create
    @star = Star.create!(params[:star])
    redirect_to star_path(@star)
  rescue ActiveRecord::RecordInvalid
    render :action => 'new'
  end

  def new
    @star = Star.new
  end

> I'm new to all the mocking/stubbing business so my head spins are turning
> into headaches. :)

Hope that helps.

David

> Dylan.
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
>


More information about the rspec-users mailing list