thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.51k stars 912 forks source link

have_one_attached matcher is failing #1362

Closed glenpike closed 3 years ago

glenpike commented 3 years ago

Loving your library for testing, but I have the following issue.

Given I have run the ActiveStorage migration and I have the following model setup:

class JobseekerDetail < ApplicationRecord
  belongs_to :user
  has_one_attached :photo
  #...
end

I would expect the following assertions to both pass:

RSpec.describe JobseekerDetail, type: :model do
  it { is_expected.to have_one(:photo_attachment) }
  it { is_expected.to have_one_attached(:photo) }
end

I get the following output from my tests:

Failures:

  1) JobseekerDetail associations is expected to have one attached :photo
     Failure/Error: it { is_expected.to have_one_attached(:photo) }
       expected #<JobseekerDetail:0x000056258862b570> to respond to `has_one_attached?`
     # ./spec/models/jobseeker_detail_spec.rb:38:in `block (3 levels) in <top (required)>'

The console says:

2.6.6 :002 > JobseekerDetail.reflect_on_all_associations(:has_one).map(&:name)
 => [:photo_attachment, :photo_blob] 

If I'm writing my test wrong, please can someone add some example 'test' documentation to (https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_record/have_attached_matcher.rb) similar to the other files?

Thanks

mcmire commented 3 years ago

Hi @glenpike. Which version of shoulda-matchers are you using?

glenpike commented 3 years ago

Hi, apologies, I had not updated my Gems correctly. Was trying to run 4.4.1 - working now. Thanks.

mcmire commented 3 years ago

No problem!