开发者

Rspec & Paperclip: Broken with attr_accessible?

I'm having a problem getting my rspec controller tests to pass with attr_accessible in Rspec...but not from the console.

post :create, :banner => valid_attributes

fails, but

Banner.create!(valid_attributes) is accepted.

If I take out attr_accessible from the banners model, or I take out the validates_attachment_presence :bannerimage, it works. Have tried adding bannerimage_attributes and the four paperclip-generated :bannerimage columns to my attr_accessible- no joy. Tried taking out other paperclip validators (content type, size) - still no joy. Any suggestions very appreciated - I'm completely at a loss.

The relevant code is here:

Relevant bits of RSPEC test:

def valid_attributes
  demo_image = File.open(File.join(Rails.root, "spec", "samples", "laptop1.jpg"))
  {
    :name => 'Test Spec Banner',
    :bannerimage => demo_image
  }
end


describe "POST create" do
  describe "with valid params" do
    it "creates a new Banner" do
      expect {
        post :create, :banner => valid_attributes
      }.to change(Banner, :count).by(1)
  end
end

Model:

class Banner < ActiveRecord::Base
  attr_accessible :name, :url, :bannerimage

  has开发者_如何学C_attached_file :bannerimage, :styles => { :full => "960x", :thumb => "100x" }

  validates_attachment_content_type :bannerimage, :content_type => [ 'image/jpg', 'image/jpeg', 'image/gif', 'image/png'], :message => 'file must be a gif, jpeg or png image'

  validates_attachment_size :bannerimage, :less_than => 3.megabytes

  validates_presence_of :name
  validates_attachment_presence :bannerimage
  validates_uniqueness_of :name

  has_many :pages, :dependent => :nullify

  def to_s
    name
  end
end

Edit: Banner can be created through site.

Relevant controller code below. No before/after calls, just a standard restful create.

  def create
    @banner = Banner.new(params[:banner])

    if @banner.save
      redirect_to admin_banner_url(@banner), notice: 'Banner was successfully created.'
    else
      render action: "new"
    end
  end


There doesn't seem to be any obvious problem with your code that might cause this. One thing you should note is that you have a uniqueness constraint on name, but valid_attributes always returns the same name. That's not where your problem is, I'm just mentioning it as something to keep in mind.

I can only give you advice on how to try and debug this yourself. You can access the newly created banner in the spec as assigns(:banner). So, you could do something like this:

describe "POST create" do
  describe "with valid params" do
    it "creates a new Banner" do
      expect {
        post :create, :banner => valid_attributes
        pp assigns(:banner).errors
      }.to change(Banner, :count).by(1)
  end
end

This will dump the banner's errors to the console where the tests are running. If the banner is not being saved, this should mean it's not valid, so checking what errors are generated is the first thing to do. You could also dump the entire banner with pp assigns(:banner) to see all of its attributes.

Sorry for not being more helpful. I would have written this in a comment, but I needed the nice formatting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜