我有一个 rspec 功能规范,它正在测试图像是否显示在页面上。由于某种疯狂的原因,我在我的功能规范中设置的图像似乎在我的控制器中不可用,在我的测试环境中。我正在使用“puts”语句来查看图像在到达视图之前是否在我的控制器中可用。
我的规格:
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