[rspec-users] how to mock active record relationships

Christoph ---- lists at ruby-forum.com
Wed Oct 21 01:40:23 EDT 2009


Hi
I am new to Rspec and try to mock my controller that looks like

 def create
    @friendship = current_user.friendships.build(:friend_id =>
params[:friend_id])
    if @friendship.save
      flash[:notice] = "Added friend."
      render :text => flash[:notice]
    else
      flash[:error] = "Error occurred when adding friend."
      render :text => flash[:notice]
    end
  end

how can I mock
 current_user.friendships.build ?

I tried

before(:each) do
    @friend_ship = mock_model(Friendship, :friend_id => 1947284801,
:user_id => 134245544)
    controller.stub!(:current_user).should_receive('friendships').and_return(@friend_ship)
end

but get
You have a nil object when you didn't expect it! The error occurred
while evaluating nil.friendship
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list