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.
我正在创建一个与数据库交互的GUI应用程序,因此我需要对我的RSpec测试进行fixture管理。我使用sqlite数据库,并将编写一个类,该类将使用直接SQL操作数据。我需要测试它的数据库交互功能。
I couldn't find any libraries which could do 2 basic things when I run RSpec tests:
当我运行RSpec测试时,我找不到任何可以做两件基本事情的库:
- 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):
已经有成千上万的博客文章和手册清楚地解释了如何在任何版本的Rails中使用factory - girl,但是没有一个没有Rails。我开始四处挖掘,这就是我所拥有的(注意我不使用rails和它的组件):
spec/note_spec.rb:
规范/ 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:
规范/ factories.rb:
Factory.define :note do |f|
f.body 'body of a note'
f.title 'title of a note'
end
lib/note.rb:
lib / note.rb:
class Note
attr_accessor :title, :body
end
When I run rspec -c spec/note_spec.rb
I get following:
当我运行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)>'
Questions:
问题:
- Is it possible at all to use FactoryGirl without Rails and Rails libraries like ActiveModel/ActiveRecord?
- 有没有可能在没有Rails和Rails库(比如ActiveModel/ActiveRecord)的情况下使用FactoryGirl ?
- Do I have to subclass my
Note
class from particular class, since FactoryGirl is looking forsave!
method? - 我是否必须从特定的类中子类化我的Note类,因为factory - girl正在寻找save!方法?
- Are there any other more viable solutions besides FactoryGirl?
- 除了工厂女孩还有其他更可行的解决方案吗?
I'm totally new to Ruby/RSpec/BDD, so any help will be greatly appreciated ;)
我对Ruby/RSpec/BDD完全陌生,所以任何帮助都将得到极大的赞赏。
1 个解决方案
#1
9
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.
默认情况下,factory_girl创建保存的实例。如果您不想将对象保存到数据库,那么可以使用构建方法创建未保存的实例。
require 'spec_helper'
require 'note'
describe Note do
it "should return body" do
@note = Factory.build(:note)
note.body.should == 'body of a note'
end
end
See "Using Factories" in the Getting Started file.
参见“使用工厂”中的“开始文件”。
#1
9
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.
默认情况下,factory_girl创建保存的实例。如果您不想将对象保存到数据库,那么可以使用构建方法创建未保存的实例。
require 'spec_helper'
require 'note'
describe Note do
it "should return body" do
@note = Factory.build(:note)
note.body.should == 'body of a note'
end
end
See "Using Factories" in the Getting Started file.
参见“使用工厂”中的“开始文件”。