是否可以在没有 Rails 的情况下使用 FactoryGirl?
我正在创建一个与数据库交互的 GUI 应用程序,因此我需要为 RSpec 测试进行夹具管理。我使用 sqlite 数据库,并且将编写一个类,它将使用直接 SQL 操作数据。我需要测试它的数据库交互功能。
当我运行 RSpec 测试时,我找不到任何可以执行 2 项基本操作的库:
- 清除数据库或其中的特定表
- 将特定数据加载到其中,以便我可以在测试中使用该数据
已经有数万篇博客文章和手册它清楚地解释了如何将 FactoryGirl 与任何版本的 Rails 一起使用,但没有人没有它。我开始挖掘,这就是我所拥有的(请注意,我不使用 Rails 及其组件):
spec/note_spec.rb:
require 'spec_helper'
require 'note'
describe Note do
it "should return body" do
@note = Factory(:note)
note.body.should == 'body of a note'
end
end
spec/factories.rb:
Factory.define :note do |f|
f.body 'body of a note'
f.title 'title of a note'
end
lib/note.rb:
class Note
attr_accessor :title, :body
end
当我运行 rspec -c spec/note_spec.rb
时,我得到以下信息:
F
Failures:
1) Note should return body
Failure/Error: @note = Factory(:note)
NoMethodError:
undefined method `save!' for #<Note:0x8c33f18>
# ./spec/note_spec.rb:6:in `block (2 levels) in <top (required)>'
问题:
- 是否可以在没有 Rails 和 Rails 库的情况下使用 FactoryGirl像 ActiveModel/ActiveRecord 一样?
- 由于 FactoryGirl 正在寻找
save!
方法,我是否必须从特定类中继承我的Note
类? - 除了FactoryGirl还有其他更可行的解决方案吗?
我对 Ruby/RSpec/BDD 完全陌生,所以任何帮助将不胜感激;)
I'm creating a GUI application which interacts with database so I need fixture management for my RSpec tests. I use sqlite database and am going to write a class which will manipulate data with straight SQL. I need to test it's database interaction functionality.
I couldn't find any libraries which could do 2 basic things when I run RSpec tests:
- Clear database or particular tables in it
- Load specific data into it so I can use that data in my tests
There are already ten thousands of blog posts and manuals which clearly explain how to use FactoryGirl with any version of Rails but no one without it. I started digging around and this is what I have (note that I don't use rails and it's components):
spec/note_spec.rb:
require 'spec_helper'
require 'note'
describe Note do
it "should return body" do
@note = Factory(:note)
note.body.should == 'body of a note'
end
end
spec/factories.rb:
Factory.define :note do |f|
f.body 'body of a note'
f.title 'title of a note'
end
lib/note.rb:
class Note
attr_accessor :title, :body
end
When I run rspec -c spec/note_spec.rb
I get following:
F
Failures:
1) Note should return body
Failure/Error: @note = Factory(:note)
NoMethodError:
undefined method `save!' for #<Note:0x8c33f18>
# ./spec/note_spec.rb:6:in `block (2 levels) in <top (required)>'
Questions:
- Is it possible at all to use FactoryGirl without Rails and Rails libraries like ActiveModel/ActiveRecord?
- Do I have to subclass my
Note
class from particular class, since FactoryGirl is looking forsave!
method? - Are there any other more viable solutions besides FactoryGirl?
I'm totally new to Ruby/RSpec/BDD, so any help will be greatly appreciated ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,factory_girl 创建保存的实例。如果您不想将对象保存到数据库,可以使用
build
方法创建未保存的实例。请参阅入门文件中的“使用工厂”。
By default factory_girl creates saved instances. If you don't want to save the object to the database, you can create unsaved instances using the
build
method.See "Using Factories" in the Getting Started file.