[rspec-users] When to stub_model?

Lee Lee.Longmore at googlemail.com
Wed May 20 07:03:43 EDT 2009

Thanks Matt. Very helpful.

I have tried out stub_model. The constructor of one of my models
(Namespace) requires a mandatory :name attribute to be provided. So
when I try to stub_model it - e.g. representative_namespace =
stub_model(Namespace) - I get the following spec error.

RuntimeError in 'ContextsController POST create authenticated user
should assign a representative namespace to the new context'
A namespace must be assigned a valid name
/Users/leelon/work/cbox/app/models/namespace.rb:643:in `set_name'
/Users/leelon/work/cbox/app/models/namespace.rb:64:in `initialize'

Is there a way around this while still making :name a mandatory
requirement of the constructor? I have tried stubbing out :name in the
call to stub_model but this doesn't help:

representative_namespace = stub_model(Namespace, :name => 'A name')


More information about the rspec-users mailing list