Rails 3.2 has-and-belongs-to-many update association unit test
In my 'notification' model I have a method (notification and contact
models both has and belongs to many):
def self.update_contact_association(contact, notification) unless contact
== nil notification.contacts.clear c = Contact.find(contact)
notification.contacts << c end end
that updates the association between a specific notification and contact.
It takes a notification object(row) and a list of contact ids. The method
works fine, given a single contact id 1 and a notification with the id of
4 updates the table should and will look like this:
notification_id contact_id 4 1
The problem comes in when trying to write a unit test to properly test
this method. So far I have:
test 'update_contact_association' do notification = Notification.find(4)
contact = Contact.find(1)
Notification.update_contact_association([contact.id], notification) end
Running the test method causes no errors, but the test database is not
updated to look like the above example, it is just blank. I'm pretty sure
I need to use a save or update method to mimic what the controller is
doing, but I'm not sure how. I just need the unit test to properly update
the table so I can go ahead and write my assertions. Any ideas would be
greatly appreciated for I need to test several methods that are very
similar/the same as this one.
Thank you!
No comments:
Post a Comment