ruby-on-rails – ‘= undefined方法`build’中的rspec-rails和f
发布时间:2020-12-16 23:26:03 所属栏目:百科 来源:网络整理
导读:我有一个带有FactoryGirl和rSpec的新Rails 4项目.在我的spec_helper.rb中,我有: # lots of stuffRSpec.configure do |config| # more stuff config.include FactoryGirl::Syntax::Methodsend 我还删除了此文件中的rspec / autorun require. 一个简单的规范
我有一个带有FactoryGirl和rSpec的新Rails 4项目.在我的spec_helper.rb中,我有:
# lots of stuff RSpec.configure do |config| # more stuff config.include FactoryGirl::Syntax::Methods end 我还删除了此文件中的rspec / autorun require. 一个简单的规范: require 'spec_helper' describe User do build(:user) end 有一个简单的工厂: FactoryGirl.define do factory :user do email "somename@someplace.com" end end 失败,显示以下消息. `block in <top (required)>': undefined method `build' for #<Class:0x007fd46d0e3848> (NoMethodError) 但是,如果我明确限定了规范中的构建,它会通过: require 'spec_helper' describe User do FactoryGirl.build(:user) end 我能做什么,所以我不必每次都添加FactoryGirl? 解决方法
传递给config.include的方法仅包含在RSpec中的it,let,before和after之后,而不是在describe的顶层.因为那时你通常需要设置你的设置和测试逻辑,实际上它并不是真正的问题.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |