unit testing - Rails 4: Why do fixtures make this test fail? -
i have test below , if there fixtures model fails total_unapproved , new_total being equal instead of new_total being 1 less. if remove loading fixtures in test_helper.rb or comment them out runs expect.
here's class function sets approvals true. works.
def inviterequest.approve_invites(number) inv_reqs = inviterequest.where("approved = ?", false).first(number) inv_reqs.each |inv_req| inv_req.approved = true inv_req.save inv_req.send_approved_email end end
here's test calls above function.
require 'test_helper' class inviterequesttest < activesupport::testcase test "class method approve_invites(number) should approve 'number' inviterequests" # ensure there @ least 1 instance inv_req = inviterequest.create(email: "hobojoe@test.com") # set inviterequests.approved false inviterequest.all.each {|r| r.approved = false; r.save} total_unapproved = inviterequest.where("approved = ?", false).count rails.logger.info "\nunapproved before: #{total_unapproved}" inviterequest.approve_invites(1) new_total = inviterequest.where("approved = ?", false).count rails.logger.info "unapproved after: #{new_total}\n" assert_equal total_unapproved - 1, new_total end end
any idea why? i'm not using fixtures in other tests maybe someday.
my fixtures weren't valid , changing them fixed problem. i'm still not sure how things failing though. fixtures looked this:
one: email: mystring two: email: mystring
this fail uniqueness validation , not save i'm not sure why newly created model wouldn't have 'approved' set true , still saved since it's correct.
anyway, changing fixtures fixed things.
one: email: someguy@example.com two: email: somegirl@example.com
Comments
Post a Comment