文件上传Rspec集成否定测试失败

时间:2012-05-23 15:13:50

标签: ruby-on-rails rspec paperclip integration-testing

我有几个自定义验证器,我用来验证回形针附件是一个图像,而不是超过5MB。一切都按预期工作

我对一件事情有点好奇。上传失败后,我的集成测试检查天气user.avatar为零,并且它返回了一些随机的内容。

测试:

 it 'Does not allow a non-image file to be uploaded as an avatar' do
   visit edit_user_path(@user)
   attach_file('user_avatar', File.dirname(__FILE__)+'/users_spec.rb')
   click_button 'Update'
   page.should have_content "Avatar must be an image"
   @user.reload
   @user.avatar.should be_nil
 end

结果:

1) Users Avatars Does not allow a non-image file to be uploaded as an avatar
 Failure/Error: @user.avatar.should be_nil
   expected: nil
        got: /avatars/original/missing.png
 # ./spec/requests/users_spec.rb:147:in `block (3 levels) in <top (required)>'

我猜这是某种默认占位符回形针在那里,但是当我转到用户show页面时,@user.avatar正如预期的那样nil。是什么给了什么?

我也试过了同样的结果

@user.should_not have_attached_file(:avatar)

鉴于它是回形针自己的匹配器,看起来应该可行,但它也失败了

在几个put语句之后,看起来@user.avatar在完成任务之前会产生/avatars/original/missing.png,所以我想我至少可以确定它没有改变。

1 个答案:

答案 0 :(得分:1)

啊哈!文件?如果文件附加到给定字段,则method返回true,因此以下代码为我提供了我想要的测试:

@user.avatar.file?.should be_false