【发布时间】:2015-02-09 10:38:34
【问题描述】:
我想在每次创建用户时删除FactoryGirl.build(:user),所以我添加了这些行:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
到spec_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
【问题讨论】:
标签: ruby-on-rails unit-testing testing rspec rspec-rails