0

スペック helper.rb

# 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/rails'
require 'capybara/rspec'
require 'factory_girl_rails'
# 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 }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|

end

factory.rb

FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end

###_create.rb

class Create < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password

      t.timestamps
    end
  end
end

password_resets_spec.rb

require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create(:user)
        visit "/users"
    end
end

これで、スペック ヘルパー、ファクトリ、User インスタンスを作成するためのデータベース マイグレーション、そして最も重要な password_resets_spec.rb ができました。

それを実行すると、ユーザー インスタンスがテスト ユーザー テーブルに作成されることが期待されます。

id | email | password | created_at | updated_at

1 | example@hotmail.com | password | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |

カピバラが既存のルートを閲覧し、障害が発生しないように。

私は一度実行しましたが、これは失敗しませんでしたが、rspec は 23 の例を経験しました。これは私のテーブルで何が起こるかです:

    id | email | password | created_at | updated_at

    1 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    2 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    3 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    4 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    5 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    6 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    7 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    8 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    9 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
   10 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
   11 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |

同じテストをもう一度実行すると、このエラーが発生します

UsersController GET index assigns all users as @users

テーブルは 2 倍の大きさになり、23 個の同一の「MyString」エントリがあります。一体何が起こっているのですか?

私が取り除く場合

visit "/users"

それはすべて期待どおりに機能します。すべての。では、カピバラは何をしているのでしょう?11 個の「MyString」エントリを作成するのはなぜですか? 「MyString」はどこから来たのですか? とても奇妙。

4

0 に答える 0