2012-03-04 38 views
0

爲什麼我會收到這些驗證錯誤? :( 任何想法帶嵌套上下文的RSpec電子郵件驗證錯誤

這些都是我的驗證錯誤 - 他們都是現已經採取了... 我的錯誤:

20:33:25 guard.1  | 
20:33:25 guard.1  | Failures: 
20:33:25 guard.1  | 
20:33:25 guard.1  | 1) BookmarkImporter should extract all links 
20:33:25 guard.1  |  Failure/Error: let(:user) { create(:user) } 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/lib/bookmark_importer_spec.rb:2:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/lib/bookmark_importer_spec.rb:11:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/lib/bookmark_importer_spec.rb:14:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 2) BookmarkImporter should assign bookmarks to the unsorted board of the user 
20:33:25 guard.1  |  Failure/Error: let(:user) { create(:user) } 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/lib/bookmark_importer_spec.rb:2:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/lib/bookmark_importer_spec.rb:11:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/lib/bookmark_importer_spec.rb:21:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 3) Board must have a name 
20:33:25 guard.1  |  Failure/Error: board = build(:board, :name => "") 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/models/board_spec.rb:5:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 4) Board is destroyable by default 
20:33:25 guard.1  |  Failure/Error: board = create(:board) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/models/board_spec.rb:10:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 5) Bookmark must have a title 
20:33:25 guard.1  |  Failure/Error: bookmark = build(:bookmark, :title => "") 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/models/bookmark_spec.rb:10:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 6) Bookmark must have a url 
20:33:25 guard.1  |  Failure/Error: bookmark = build(:bookmark, :url => "") 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/models/bookmark_spec.rb:15:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 7) User should have a unsorted board 
20:33:25 guard.1  |  Failure/Error: let(:user) { create(:user) } 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/models/user_spec.rb:7:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 8) User should not be able to destroy the unsorted board 
20:33:25 guard.1  |  Failure/Error: let(:user) { create(:user) } 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/models/user_spec.rb:22:in `block (2 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 9) Bookmarks GET /bookmarks when user is logged in and has no bookmarks, should display a getting started help if the user has no bookmarks 
20:33:25 guard.1  |  Failure/Error: page.should have_content("Get started:") 
20:33:25 guard.1  |  Capybara::ElementNotFound: 
20:33:25 guard.1  |  Unable to find xpath "/html" 
20:33:25 guard.1  |  # (eval):2:in `text' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:23:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 10) Bookmarks GET /bookmarks when user is logged in and has bookmarks, main page should display the unsorted bookmarks board 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 11) Bookmarks GET /bookmarks when user is logged in and has bookmarks, main page should display all boards 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 12) Bookmarks GET /bookmarks when user is logged in and has bookmarks, main page should have a way to access account settings 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 13) Bookmarks GET /bookmarks when user is logged in and has bookmarks, board should display bookmarks inside 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 14) Bookmarks GET /bookmarks when user is logged in and has bookmarks, board should allow deletion 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 15) Bookmarks GET /bookmarks when user is logged in and has bookmarks, bookmarks should allow deletion 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 16) Bookmarks GET /bookmarks when user is logged in and has bookmarks, bookmarks should allow dragging and dropping between boards 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:25 guard.1  | 17) Bookmarks GET /bookmarks when user is logged in and has bookmarks, archiving should be able to show archive buttons 
20:33:25 guard.1  |  Failure/Error: user = create(:user) 
20:33:25 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:25 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:25 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:25 guard.1  | 
20:33:26 guard.1  | 18) Bookmarks GET /bookmarks when user is logged in and has bookmarks, archiving should be able to show archived bookmarks 
20:33:26 guard.1  |  Failure/Error: user = create(:user) 
20:33:26 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:26 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:26 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:26 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:26 guard.1  | 
20:33:26 guard.1  | 19) Bookmarks GET /bookmarks when user is logged in and has bookmarks, archiving should hide the bookmark when archived 
20:33:26 guard.1  |  Failure/Error: user = create(:user) 
20:33:26 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:26 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:26 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:26 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:26 guard.1  | 
20:33:26 guard.1  | 20) Bookmarks GET /bookmarks when user is logged in and has bookmarks, archiving should display the bookmark when unarchived 
20:33:26 guard.1  |  Failure/Error: user = create(:user) 
20:33:26 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:26 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:26 guard.1  |  # ./spec/requests/bookmarks_spec.rb:14:in `block (4 levels) in <top (required)>' 
20:33:26 guard.1  |  # ./spec/requests/bookmarks_spec.rb:35:in `block (5 levels) in <top (required)>' 
20:33:26 guard.1  | 
20:33:26 guard.1  | 21) Landing GET/should redirect to bookmarks if user is logged in 
20:33:26 guard.1  |  Failure/Error: user = create(:user) 
20:33:26 guard.1  |  ActiveRecord::RecordInvalid: 
20:33:26 guard.1  |  Validation failed: Email has already been taken, Email has already been taken 
20:33:26 guard.1  |  # ./spec/requests/home_spec.rb:9:in `block (3 levels) in <top (required)>' 
20:33:26 guard.1  | 

我的規格有大量的測試,要注意是讓(這似乎是運行一次以上) 我的規格:

require 'spec_helper' 

describe "Bookmarks" do 
    describe "GET /bookmarks" do 
    context "when user is not logged in" do 
     it "should redirect a to home" do 
     get bookmarks_path 
     response.should redirect_to(root_url) 
     end 
    end 

    context "when user is logged in" do 
     let(:user) { 
     user = create(:user) 
     user.confirm! 
     login_as(user, :scope => :user) 
     user 
     } 

     context "and has no bookmarks," do 
     it "should display a getting started help if the user has no bookmarks" do 
      get bookmarks_path 
      page.should have_content("Get started:") 
      page.should have_content("Import bookmarks:") 
      page.should have_content("Drag the bookmarklet:") 
     end 
     end 

     context "and has bookmarks," do 

     before(:all) do 
      bookmark_file = File.open(Rails.root.join("spec", "support", "bookmarks.html")) 

      # the let you defined above for "user" is available here, but it needs to be in an RSpec block 
      BookmarkImporter.new(bookmark_file, user) 
     end 

     describe "main page" do 
      it "should display the unsorted bookmarks board" do 
      visit bookmarks_path 
      page.should have_content("Unsorted") 
      end 

      it "should display all boards" do 
      pending 
      end 

      it "should have a way to access account settings" do 
      pending 
      end 

     end 

     describe "board" do 
      it "should display bookmarks inside" do 
      pending 
      end 

      it "should allow deletion" do 
      pending 
      end 
     end 


     describe "bookmarks" do 
      it "should allow deletion" do 
      pending 
      end 

      it "should allow dragging and dropping between boards" do 
      pending 
      end 
     end 

     describe "archiving" do 
      it "should be able to show archive buttons" do 
      pending 
      end 

      it "should be able to show archived bookmarks" do 
      pending 
      # archived bookmarks should be hidden 
      # archived bookmarks should be hidden and shown after clicking the link 
      end 

      it "should hide the bookmark when archived" do 
      pending 
      end 

      it "should display the bookmark when unarchived" do 
      pending 
      end 
     end 
     end # end user has bookmarks context 
    end #end user logged in context 
    end 
end 

我設置的用戶電子郵件是一個序列...所以它不應該得到驗證錯誤... 我工廠:

# Read about factories at http://github.com/thoughtbot/factory_girl 

FactoryGirl.define do 
    factory :board do 
    name "MyString" 
    user 
    end 

    factory :bookmark do 
    title "bookmark title" 
    url "http://www.google.com" 
    type "" 
    board 
    end 

    factory :user do 
    sequence(:email) {|n| "person#{n}@example.com" } 
    password "password" 
    #encrypted_password "$2a$10$qntR5INbkjtRZ.wDEpsNVemyO6oGknq9VyGt.8El4Tg1roHqs7mK2" 
    end 
end 

以防萬一...... 我的規格幫手:

if(ENV["RUN_COVERAGE"]) 
    require 'simplecov' 
    SimpleCov.start 'rails' do 
    add_filter "/spec/" 
    end 
    puts "Running coverage tool\n" 
end 
require 'rubygems' 
require 'spork' 
#uncomment the following line to use spork with the debugger 
#require 'spork/ext/ruby-debug' 

Spork.prefork do 
    # Loading more in this block will cause your tests to run faster. However, 
    # if you change any configuration or code from libraries loaded here, you'll 
    # need to restart spork for it take effect. 

    # This file is copied to spec/ when you run 'rails generate rspec:install' 
    ENV["RAILS_ENV"] ||= 'test' 
    require File.expand_path("../../config/environment", __FILE__) 
    require 'rspec/rails' 
    require 'rspec/autorun' 
    require 'capybara/rspec' 
    require 'capybara/rails' 


    RSpec.configure do |config| 
    # ## Mock Framework 
    config.mock_with :rr 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    config.include Factory::Syntax::Methods 

    config.include Warden::Test::Helpers, :type => :request 
    Warden.test_mode! 
    config.after(:each) { Warden.test_reset! } 
    end 

end 

Spork.each_run do 
    # This code will be run each time you run your specs. 

    # Reload models on each run 
    require 'factory_girl_rails' 
    # reload all the models 
    Dir["#{Rails.root}/app/models/**/*.rb"].each do |model| 
    load model 
    end 

    # Reload Factories on each run 
    FactoryGirl.factories.clear 
    FactoryGirl.find_definitions 

    # Requires supporting ruby files with custom matchers and macros, etc, 
    # in spec/support/ and its subdirectories. 
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 
end 

回答

1

before(:all) RSpec中的塊不會在事務內部運行,這就是爲什麼在處理完整個測試套件後您的用戶留在測試數據庫中的原因。

考慮使用after(:all)塊,您必須手動清理適當的表,或使用database_cleaner,您可以配置它以在測試運行之前截斷表。

+0

我跑了一次database_cleaner,並得到了很多關於已經運行的交易的警告...... 評論出來(並且將before(:all)更改爲之前(:each))之後,一切都按預期工作! – ibash 2012-03-04 18:11:42

0

create FactoryGirl語法可以是:

Factory(:user) 

FactoryGirl.create(:user) 

我懷疑呼叫create(:user)沒有做你認爲正在做的事情。更新您的let到:

let(:user) { 
    user = Factory(:user) 
    user.confirm! 
    login_as(user, :scope => :user) 
    user 
    } 

注意let都會被調用它是由測試所需要的時間。

+0

注意'RSpec.configure'塊中的'config.include Factory :: Syntax :: Methods'。 – 2012-03-04 09:51:48