I would like some feedback from others on the "right" way to do this.
I'm using RSpec testing a controller using Devise for authentication and CanCan for permissions. I'm loading a resource that loads data scoped by an account. Like this...
def show
@asset = current_user.account.assets.find(param[:id])
end
I got it all to work, but I didn't do it the "right" way. Or at least what I read everywhere that says I should mock and stub out everything about the user. However, in examples like this, I feel like I'm fully white-box testing because the stubs have to do too much. The controller authenticates the user, checks their abilities (via CanCan) and all this just to find a record. The user can have multiple roles assigned so it isn't just an :admin? method needed.
For simplicity, I just do it using a Factory (via factory_girl) to create the asset like this...
describe "GET show" do
it "assigns the requested asset as @asset" do
asset = Factory(:asset, :account => @current_user.account)
get :show, :id =>
asset.id assigns[:asset].should == asset
end
end
This is working for me and to me it seems clean and easy to maintain. In my search to get this working, everything I read said I should stub it. I had a hard time even figuring out how I would stub all that out.
At the RubyWebConf, the critique I heard against the factory approach was
that it gets written to the DB so it is slower especially as the test
suite grows. In this particular project I'm not concerned about testing
speed.
Am I being stubborn in just using a factory? Is there an easy way to do it the "right" way?
Any help or tips are appreciated.
Thanks,
-Mark E.