【问题标题】:rspec ActiveStorage::Attachment.count wont changerspec ActiveStorage::Attachment.count 不会改变
【发布时间】:2021-08-28 20:56:46
【问题描述】:

我尝试通过更新我的Shop 模型的company_logo 来使用rspec 测试我的API。

当我使用 Postman 向 API 发出请求并附加文件时,它会正确保存。所以 Controller#action 似乎工作得很好,我也得到了一个 URL:

{
    "name": "Sipes-Runte",
    "xml_feed": null,
    "slug": "sipes-runte",
    "url": "http://beier.co/ivey.kilback",
    "company_logo": {
        "url": "http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--f8115e92010b47d1a657825a769b221e00f09b9d/thomasz%20portrait.jpeg",
        "signed_id": "eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--f8115e92010b47d1a657825a769b221e00f09b9d"
    }

这是控制器:

  def update
    if @shop.update(shop_params)
      # following line is optional to see if its working (It didn't)
  @shop.company_logo.attach(params.dig(:shop, :company_logo)) if logo_params.present? # 
      render json: @shop, serializer: ShopSerializer
    else
      render json: @shop.errors, status: :ok
    end

但我不知道为什么我的 rspec 测试失败了:

方法一:

   subject { patch shop_url(shop), params: params, headers: valid_headers(admin), as: :json }

        let(:params) { { shop: { name: "SHopName", company_logo: company_logo } } }
        let(:shop) { Shop.create! valid_attributes }
        let(:company_logo) { fixture_file_upload('400x400.png') }

        it 'returns status created' do
          subject
          expect(response).to have_http_status :ok
        end # works == GREEN

        it 'creates a blob ' do
          expect { subject }.to change { ActiveStorage::Blob.count }.from(0).to(1)
        end
      end
# FAILS with
# expected `ActiveStorage::Blob.count` to have changed from 0 to 1, but did not change

第二种方法:

        it "updates the company_logo" do
          subject
          expect(shop.company_logo).to be_attached
        end
# FAILS with:
# to be truthy, got false

这是我的第三种方法:

      it "updates the company_logo" do
        shop = Shop.create! valid_attributes
        company_logo = fixture_file_upload('400x400.png', 'image/png')
        expect {
          patch shop_url(shop),
                params: { shop: { company_logo: company_logo } }, headers: valid_headers(shop.user), as: :json
        }.to change(ActiveStorage::Attachment, :count).by(1)
      end
# FAILS with
# expected `ActiveStorage::Attachment.count` to have changed by 1, but was changed by 0
# I got that snippet from this tutorial: https://www.zweitag.de/blog/active-storage-rails-api-apps/

我可以看到,参数包含在请求中(tempfileoriginal_filename 等都存在...

有什么想法让这个绿色环保吗?

我只想确定文件是否已保存。

【问题讨论】:

    标签: rspec rspec-rails rails-activestorage


    【解决方案1】:

    好的,我想我明白了:

    我必须删除 as: :json 以及修改标题:

    来自:

      def valid_headers(user = self.user)
        {
          "Authorization" => token_generator(user.id),
          "Content-Type" => "application/json",
          "Accept" => "application/json"
        }
      end 
       subject { patch shop_url(shop), params: params, headers: valid_headers(user), as: :json }
    

      def valid_multipart_headers(user = self.user)
        {
          "Authorization" => token_generator(user.id)
        }
      end
    
       subject { patch shop_url(shop), params: params, headers: valid_multipart_headers(user) }
    

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2021-03-14
      • 2011-10-10
      • 2021-05-02
      • 2019-12-27
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多