如何从Rspec中的模型规范测试HTTP请求?

时间:2022-08-24 04:02:34

I am uploading an image to my Imagecollection model in using CarrierWave, and would like to test that when I upload the image, it is actually available online. And that when I delete the image, it is actually removed.

我正在使用CarrierWave上传一个图像到我的Imagecollection模型,并且想测试当我上传图片时,它实际上是可以在网上找到的。当我删除图像时,它实际上被删除了。

I'm using an S3 backend, so i'd like to test this in the model itself, without having to have any controller dependencies, or run integration tests. So I would need to construct the url, Issue an HTTP reequest, and tests its return code. This code doesn't work, but is there a way to do something similar to the following:

我使用的是S3后端,所以我想在模型本身中测试这个,而不需要任何控制器依赖项,或者运行集成测试。因此,我需要构造url,发出HTTP reequest,并测试它的返回代码。这段代码不起作用,但是否有办法做类似的事情:

describe "once uploaded" do
  subject {Factory :company_with_images} 

  it "should be accessible from a URL" do
    image_url = subject.images.first.image.url
    get image_url                                   # Doesn't work
    response.should be_success                      # Doesn't work
  end
end

EDIT:

编辑:

I ended up adding this to my Gemfile

最后我把这个添加到Gemfile中。

gem rest-client

And using the :fog backend for my tests. Ideally, I could change the backend during the test with something like

并使用:fog后端进行测试。理想情况下,我可以在测试期间用类似的东西来改变后端。

before do
  CarrierWave.configure do |config|
     config.storage = :fog
  end
end

describe tests
end

after do
  CarrierWave.configure do |config|
     config.storage = :end
  end
end

But that doesn't seem to actually do anything.

但这似乎并没有起到什么作用。

describe "once uploaded" do
  describe "using the :fog backend" do
    subject {Factory :company_with_images} 

    # This test only passes beecause the S3 host is specified in the url.
    # When using CarrierWave :file storage, the host isn't specified and it
    # fails
    it "should be accessible from a URL" do
      image_url = subject.images.first.image.url
      response = RestClient.get image_url
      response.code.should eq(200)
    end
  end

  describe "using the :file backend" do
    subject {Factory :company_with_images} 

    # This test fails because the host isn't specified in the url
    it "should be accessible from a URL" do
      image_url = subject.images.first.image.url
      response = RestClient.get image_url
      response.code.should eq(200)
    end
  end
end

3 个解决方案

#1


1  

I'm not familiar with CarrierWave, you should be testing your code, not any external libraries or services it depends on. In other words, you want to test your class, not S3. I suggest mocking the calls to S3 made by your model to verify that it makes all the correct ones.

我不熟悉CarrierWave,你应该测试你的代码,而不是它依赖的任何外部库或服务。换句话说,您希望测试您的类,而不是S3。我建议用您的模型对S3的调用进行模拟,以验证它是否正确。

#2


0  

You can't test s3, unless the files are actually uploaded to s3. And in carrierwave, by default, it wont upload to s3.

您不能测试s3,除非文件实际上传到了s3。而在carrierwave中,默认情况下,它不会上传到s3。

instead, test that image_url is proper:

相反,测试image_url是正确的:

image_url = subject.images.first.image.url
image_url.should == "http://.....'

#3


0  

I ended up redefining the spec as follows

我最后重新定义了规范如下。

  describe "using the :fog backend" do
    subject {Factory :company_with_images} 

    it "should be accessible from a URL" do
      image_url = subject.images.first.image.url
      rest_response(image_url).code.should eq(200)
    end
  end

with this helper

这个助手

def rest_response url
  # https://github.com/archiloque/rest-client
  RestClient.get(url){|response, request, result| response }
end

and using the restclient gem

并使用restclient gem。

#1


1  

I'm not familiar with CarrierWave, you should be testing your code, not any external libraries or services it depends on. In other words, you want to test your class, not S3. I suggest mocking the calls to S3 made by your model to verify that it makes all the correct ones.

我不熟悉CarrierWave,你应该测试你的代码,而不是它依赖的任何外部库或服务。换句话说,您希望测试您的类,而不是S3。我建议用您的模型对S3的调用进行模拟,以验证它是否正确。

#2


0  

You can't test s3, unless the files are actually uploaded to s3. And in carrierwave, by default, it wont upload to s3.

您不能测试s3,除非文件实际上传到了s3。而在carrierwave中,默认情况下,它不会上传到s3。

instead, test that image_url is proper:

相反,测试image_url是正确的:

image_url = subject.images.first.image.url
image_url.should == "http://.....'

#3


0  

I ended up redefining the spec as follows

我最后重新定义了规范如下。

  describe "using the :fog backend" do
    subject {Factory :company_with_images} 

    it "should be accessible from a URL" do
      image_url = subject.images.first.image.url
      rest_response(image_url).code.should eq(200)
    end
  end

with this helper

这个助手

def rest_response url
  # https://github.com/archiloque/rest-client
  RestClient.get(url){|response, request, result| response }
end

and using the restclient gem

并使用restclient gem。