let (:event_category) { mock_model(EventCategory).as_null_object }
and the problem I’m having is that @event_type is coming back as EventCategory
and not EventType.
Because of this problem, I thought, eh, I didn’t know Rails did that! So I
checked use of .build in the real code and it does return EventType.
I’m obviously doing something stupidly wrong in the test but I just can’t figure
it out.
Any pointers would be greatly appreciated, thank you.
event_category is a mock_model, not an EventCategory. Since it’s
declared as_null_object, when it receives :event_types, it returns
itself, and then does the same for :build. So it’s not really even an
EventCategory: it’s a the same mock_model object, which is designed to
lie about it’s class in order to be able to work with form builders.
Thanks very much for the reply. I was aware I was dealing with a mock,
but
I thought in this instance it would fool event_types into thinking it
was a
genuine object to allow the association to be made and return EventType.
So what are my options? To add the data to the DB or to build the object
the long way round
While I have your ears, is it necessary to replicate the behaviour in
the
model spec of how a controller builds an object? i.e use .build in the
controller and use .new in the spec. I mean, to me, it doesn’t matter as
all I’m really concerned with is working with the object no matter how
it’s
built.
Great piece of software and book, by-the-way. Thanks very much for it.
Any pointers would be greatly appreciated, thank you.
event_category is a mock_model, not an EventCategory. Since it’s declared
as_null_object, when it receives :event_types, it returns itself, and then does
the same for :build. So it’s not really even an EventCategory: it’s a the same
mock_model object, which is designed to lie about it’s class in order to be able
to work with form builders.
David,
Thanks very much for the reply. I was aware I was dealing with a mock, but I
thought in this instance it would fool event_types into thinking it was a genuine
object to allow the association to be made and return EventType.
So what are my options? To add the data to the DB or to build the object the
long way round @et = EventType.new(@avalid_attributes) @et.event_category = event_category ## mock @et.save!
Depends on where this is. I try to use the db in model specs, but avoid
it in controller specs.
While I have your ears, is it necessary to replicate the behaviour in the model
spec of how a controller builds an object? i.e use .build in the controller and
use .new in the spec. I mean, to me, it doesn’t matter as all I’m really concerned
with is working with the object no matter how it’s built.
Generally speaking, I agree, but every situation is a little different.
Great piece of software and book, by-the-way. Thanks very much for it.
Thanks!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.