2012-06-02 4 views
1

내가 Minitest와 비트 주위에 놀고 있었고, 나는 아주 간단한 모델을 테스트 파일로이Minitest는 두 개의 연속 실행

에 대한 설명을 찾을 수 없습니다 동작을 발견에 다른 결과를 제공합니다 다음 : 난 후 처음으로이 코드를 실행하면

require 'minitest_helper' 

describe User do 

    before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobards", password_confirmation: "foobards") 
    end 

    describe "with admin attribute set to 'true'" do 
    before { @user.toggle!(:admin) } 

    it { @user.admin.must_equal true } 
    end 
end 

는 '레이크 DB : 테스트 : 준비', 테스트를 통과

내가 2 년 연속 시간 동안 실행

, 그것은 나를을 제공합니다 오류 :

test_0001_anonymous 0:00:00.132
ERROR SQLite3::ConstraintException: column email is not unique: INSERT INTO "users" ("admin", "created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?, ?)

는 그러나이 오류는 내가 꺼내 경우가 발생하지 않는 것

before { @user.toggle!(:admin) } 

다음과 같이 내 minitest_helper.rb은 다음과 같습니다

ENV["RAILS_ENV"] = "test" 
require File.expand_path("../../config/environment", __FILE__) 
require "minitest/autorun" 
require "capybara/rails" 
require "active_support/testing/setup_and_teardown" 

class IntegrationTest < MiniTest::Spec 
    include Rails.application.routes.url_helpers 
    include Capybara::DSL 
    register_spec_type(/integration$/, self) 
end 

class HelperTest < MiniTest::Spec 
    include ActiveSupport::Testing::SetupAndTeardown 
    include ActionView::TestCase::Behavior 
    register_spec_type(/Helper$/, self) 
end 

Turn.config.format = :outline 

내가이 경우 이해 수없는 것 버그 또는 if (더 있음직 한) 나는 무엇인가 놓치고있다. 나보다 지식이 많은 사람이 설명해 주시겠습니까?

답변

1

toggle! 메서드는 레코드를 저장합니다. 따라서 두 번째 테스트를 실행하면 이미 데이터베이스에 "[email protected]"이라는 전자 메일이있는 레코드가 하나 있습니다. 그리고 전자 메일 주소의 고유성을 보장하는 유효성 검사가 실패합니다.

대신 toggle (강타 없음)을 사용해보세요.

+0

멋진으로 ORMs의 범위에 대해 여러 전략 (트랜잭션, 절단 등)을 database_cleaner 보석이 일을하는 한 가지 방법입니다

실행하고 지원합니다. rake db : test : prepare everytime을 호출하는 것 외에 Minitest로 테스트를 실행하기 전에 깨끗한 데이터베이스/세션을 보장하는 좋은 방법을 알고 있습니까? 나는 이것이 rspec에 관한 많은 이슈가 아니라고 느낀다. – hangsu

1

각 테스트를 트랜잭션으로 랩핑하고 (변경 사항이 저장되지 않도록 롤백) 테스트간에 모든 테이블을 지울 필요가 있습니다. 하지 정크 이후의 시험을 방해 할 데이터베이스에서 왼쪽 경우 감사

관련 문제