Bir kullanıcı oluşturmak istediğimde her zaman FactoryGirl.build(:user)
kaldırmak istedim ve bu satırları ekledim:`method_missing ':` build` örnek bir grupta mevcut değil (örn. Bir `description` veya` context` bloğu)
spec_helper.rb
numaralı telefona
. Ama bu şu hatayı oluşturur:
`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)
Sonra bütün bağlam/blokları tarif kaldırıldı, ama bu hiçbir şeyi değiştirmedi. Bunlardan herhangi biriyle aynı sorunu yaşadınız mı ve nasıl düzeltebilirim?
anda benim testler böylece benziyor: Yerine
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: '[email protected]', password: 'password', password_confirmation: 'password')
user.projects << project
expect{user.destroy}.to change {Project.count}.by(-1)
end
end