views:

40

answers:

1

I've found myself doing this a lot lately... how can I improve the declaration of the paperclip fixture? (sorry, noob question I know)

class ProjectTest < ActiveSupport::TestCase
  def xxx
    project = Project.new({
      :name => 'xxx', 
      :attachment => File.new(Rails.root + 'test/fixtures/files/attachments/xxx.psd')
    })
    project.save
    project
  end





  test "should not save project without name" do
    assert xxx, "Saved project without a name"
  end

  test "should save a project with an attachment and have a valid directory" do
    assert File.directory?(xxx.directory), "Saved project without establishing a valid directory"
  end

  test "should save a project with an attachment and have a URL" do
    assert !xxx.filename.nil?, "Saved project without establishing a valid URL"
  end

  test "should save a project with an attachment and have a valid absolute file path" do
    assert File.exists?(xxx.absolute_filename), "Saved project without establishing a valid absolute filename"
  end

  test "should save a project with an attachment and have a valid base filename" do
    assert !xxx.base_filename.nil?, "Saved project without establishing a valid base filename"
  end
end
A: 

the classic way of using fixtures in rails is to create a yaml fixture file, something like this:

record1:
  name: xxxx
  attachment: test/fixtures/files/attachements/xxx.psd

record2:
  .... etc.

store this in test/fixtures/projects.yml

then at the top of your test, write:

fixtures :projects

and this will load the fixtures in your fixtures file into the database, at which time you can carry out whichever tests you want.

stephenr