无法使用回形针在测试ENV中保存文件

时间:2013-06-22 19:06:28

标签: ruby-on-rails ruby rspec paperclip

我有一个rspec功能规范,用于测试图像是否在页面上显示。出于一些疯狂的原因,似乎我在我的功能规范中设置的图像在我的控制器中没有,在我的测试环境中。我正在使用'puts'语句来查看图像在到达视图之前是否在我的控制器中可用。

Myspec:

require 'spec_helper'

feature "Add Images" do
    let(:bob) { FactoryGirl.create(:user)}

    context "user with a teammate who has a profile picture" do
        given(:jane) { FactoryGirl.create(:profile).user }
        given(:teamA) { FactoryGirl.create(:team) }
        before(:each) {
            # bob and jane become teammates
            bob.teams.create.users<<jane
            # bob adds a profile picture
            bob.profile.picture = File.new(Rails.root.join('spec', 'features', 'files', 'images.jpeg'))
            bob.save(validate: false).should be_true
        }

        scenario "try to upload a team photo" do
            sign_in_with(bob.email, bob.password)
            expect(current_path).to eq "/myprofile"
            page.should_not have_content "To register for an event, first you need to find a teammate!"
            page.body.should_not have_link "find a teammate!"
            url = bob.profile.picture.url(:tour)
            page.body.should have_xpath("//img[@src=\"#{url}\"]")


        end
    end
end

我的模特:

class Profile < ActiveRecord::Base
    belongs_to :user
    validates :first_name, :last_name, :city, :street_address, :state, :presence => true, :on => :update
    picture_options = {
        styles: {
            medium: '300x300>}',
            teamP: '96x96#',
            tour: '80x80#'
        },
        :url => "/system/:attachment/:id/:style.:extension",
    }
    picture_options.update({
        storage: :s3,
        s3_credentials: "#{::Rails.root}/config/aws.yml"
    }) if ENABLE_S3

  has_attached_file :picture, picture_options


end

我的控制员:

def show_current
    @can_register = current_user.teams.size > 0
    @profile = current_user.profile
    @team = current_user.teams.first
    puts @profile.picture?
    puts @profile.id

end

我的观点:

 <% if @profile.picture.exist? %>
     <%= image_tag @profile.picture.url(:tour), class: 'tourPersonMain', alt: 'Person Icon' %>
 <% else %>
       <%= image_tag 'iconPersonBig.png', class: 'tourPersonMain', alt: 'Person Icon' %>
 <% end %>

测试输出:

 1) Add Images user with a teammate who has a profile picture try to upload a team photo
 Failure/Error: page.body.should have_xpath("//img[@src=\"#{url}\"]")
 Capybara::ExpectationNotMet:
   expected to find xpath "//img[@src=\"/system/pictures/14/tour.jpeg?1371927924\"]" but there were no matches

0 个答案:

没有答案