Rails:如何确保测试数据库的清洁状态?

时间:2015-08-15 20:11:10

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rspec

请帮助解决问题。

工厂:

FactoryGirl.define do
  factory :album do
    sequence(:title){ |i| "title#{i}" }
    user_id 1
    closed nil
    description 'g dgd fghf ghj gj gj gj gj g'
  end
end

albums_controller_spec.rb:

require 'spec_helper'
describe AlbumsController, type: :controller do
  describe 'show action' do
    it 'render show template if user and album is found' do
      album = FactoryGirl.create(:album)
      get :show, { user_id: 1, id: album.id }
      response.should render_template('show')
      response.should render_template "layouts/application"
    end
  end  
end

专辑型号:

class Album < ActiveRecord::Base
  validates :title, presence: true, length: { maximum:  50, minimum: 3 }, uniqueness: { case_sensitive: false }
  validates :description, presence: true, length: { maximum:  600, minimum: 10 }

  belongs_to :user
  has_many :images
end

我在控制台中运行:

rspec spec

控制台显示如下:

OK
Finished in 0.28618 seconds (files took 1.91 seconds to load)
1 example, 0 failures

但我再次运行命令:

rspec spec

和控制台显示错误消息:

ERROR: record is exist!
rspec ./spec/controllers/albums_controller_spec.rb:14 # AlbumsController show action render show template if user and album is found

没有错误,我每次都要跑:

rake db:reset RAILS_ENV=test

但不方便。请告诉我,是不是每次都能输入这个命令(db:reset)?

0 个答案:

没有答案