Ruby on rails `方法u缺失';:`示例组(例如“descripe”或“context”块)上没有build

Ruby on rails `方法u缺失';:`示例组(例如“descripe”或“context”块)上没有build,ruby-on-rails,unit-testing,testing,rspec,rspec-rails,Ruby On Rails,Unit Testing,Testing,Rspec,Rspec Rails,每次创建用户时,我都想删除FactoryGirl.build(:user),因此我添加了以下几行: RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end 到spec\u helper.rb。但这会产生以下错误: `method_missing': `build` is not available on an example group (e.g. a `describe` or `contex

每次创建用户时,我都想删除
FactoryGirl.build(:user)
,因此我添加了以下几行:

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end
spec\u helper.rb
。但这会产生以下错误:

`method_missing': `build` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). (RSpec::Core::ExampleGroup::WrongScopeError)
然后我删除了所有上下文/描述块,但这并没有改变任何东西。你们有没有人遇到过同样的问题?我该如何解决

目前我的测试是这样的:

require 'rails_helper'

RSpec.describe User, type: :model do
  user = build(:user)
  project = build(:project)

  it "is valid with a firstname, lastname, email and password" do
    expect(user).to be_valid
  end

  it "is invalid without a firstname" do
    user = build(:user, name: nil)

    expect(user.valid?).to be_falsey
    expect(user.errors[:name].size).to eq(1)
  end

  it "is invalid without a lastname" do
    user = build(:user, surname: nil)

    expect(user.valid?).to be_falsey
    expect(user.errors[:surname].size).to eq(1)
  end

  it "destroys dependent projects" do
    user = User.create!(name: 'john', surname: 'doe', email: 't@example.com', password: 'password', password_confirmation: 'password')
    user.projects << project

    expect{user.destroy}.to change {Project.count}.by(-1)
  end

end
require'rails\u helper'
RSpec.description用户,类型::model do
user=build(:user)
项目=构建(:项目)
它“使用名、姓、电子邮件和密码有效”do
预期(用户)。为有效
结束
它“没有名字无效”吗
user=build(:user,name:nil)
期望(user.valid?)是错误的
预期(user.errors[:name].size.)为等式(1)
结束
它“没有姓氏无效”吗
user=build(:user,姓氏:nil)
期望(user.valid?)是错误的
expect(user.errors[:姓氏].size.)到等式(1)
结束
它“破坏依赖项目”吗
user=user.create!(姓名:约翰,姓氏:doe,电子邮件:'t@example.com,密码:'password',密码_确认:'password')
user.projects而不是:

user = build(:user)
project = build(:project)
做:

一般来说,定义外部变量以在测试中使用它们不是一个好主意,因为这可能会使您的测试顺序相关,并且极难调试。始终使用
let
语法,以便每次测试都重新初始化值

let(:user) { build(:user) }
let(:project) { build(:project) }