Ruby on rails 4 在自动化测试中使用夹具/工厂时,我是否需要首先创建模型和资源?

Ruby on rails 4 在自动化测试中使用夹具/工厂时,我是否需要首先创建模型和资源?,ruby-on-rails-4,factory-bot,Ruby On Rails 4,Factory Bot,啊,这可能是一个令人尴尬的问题,但我是新来的工厂女孩 spec/spec\u 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 'rspe

啊,这可能是一个令人尴尬的问题,但我是新来的工厂女孩

spec/spec\u 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/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
FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end
require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create :user
    end
end
spec/factories.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/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
FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end
require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create :user
    end
end
spec/password\u重置\u spec.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/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
FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end
require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create :user
    end
end
但这给了我:

uninitialized constant User

那么我需要先定义用户模型吗?

当然需要。FactoryGirl无法在没有模型的情况下创建记录。

当然可以。FactoryGirl无法创建没有模型的记录。

谢谢!出于尊重,我认为factory girl可以欺骗数据库实例。我是TDD的新手,我想你应该在开始开发应用程序之前编写所有测试。现在我看到您定义了所有模型,然后编写了所有测试,然后开发了应用程序?不,FactoryGirl基本上只调用User.create(一些属性)。我的工作流程是1。模型,2。工厂,3。测试,4。使用测试实现功能。也不要试图一次为整个应用程序编写测试。一次开发一个功能。谢谢!出于尊重,我认为factory girl可以欺骗数据库实例。我是TDD的新手,我想你应该在开始开发应用程序之前编写所有测试。现在我看到您定义了所有模型,然后编写了所有测试,然后开发了应用程序?不,FactoryGirl基本上只调用User.create(一些属性)。我的工作流程是1。模型,2。工厂,3。测试,4。使用测试实现功能。也不要试图一次为整个应用程序编写测试。一次处理一个功能。