How to deal with the ridicule of nested resources in RSpec and Rails?

I have an embedded resource for user reading lists (User has_many Reading Lists). I try to make fun of everything in my controller specifications, but I can not restrain it. Here is the code before the #show action:

@reading_lists = mock("Reading lists")
@reading_lists.stub!(:find).with("1").and_return(@reading_list)
@user = mock_model(User, :reading_lists => @reading_lists)
User.stub!(:find).with("1").and_return(@user)
get :show, :user_id => "1", :id => "1"

which is being tested:

def show
  @user = User.find(params[:user_id])
  @reading_list = @user.reading_lists.find params[:id]
end

This seems like a crazy amount of templates - is there a better way to mock this?

+3
source share
1 answer

There is no better way to mock this, but you are right to point out that this is a lot of boiler stoves. The reason is that it user.reading_lists.findis a violation of the Law of Demeter. No matter how much you see Demeter’s Law as important, taunting violations is painful.

, . , , , .

+4

All Articles