是否可以在没有 Rails 的情况下使用 FactoryGirl?

发布于 2024-10-20 08:19:07 字数 1441 浏览 3 评论 0原文

我正在创建一个与数据库交互的 GUI 应用程序,因此我需要为 RSpec 测试进行夹具管理。我使用 sqlite 数据库,并且将编写一个类,它将使用直接 SQL 操作数据。我需要测试它的数据库交互功能。

当我运行 RSpec 测试时,我找不到任何可以执行 2 项基本操作的库:

  1. 清除数据库或其中的特定表
  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)>'

问题:

  1. 是否可以在没有 Rails 和 Rails 库的情况下使用 FactoryGirl像 ActiveModel/ActiveRecord 一样?
  2. 由于 FactoryGirl 正在寻找 save! 方法,我是否必须从特定类中继承我的 Note 类?
  3. 除了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:

  1. Clear database or particular tables in it
  2. 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:

  1. Is it possible at all to use FactoryGirl without Rails and Rails libraries like ActiveModel/ActiveRecord?
  2. Do I have to subclass my Note class from particular class, since FactoryGirl is looking for save! method?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-10-27 08:19:07

默认情况下,factory_girl 创建保存的实例。如果您不想将对象保存到数据库,可以使用 build 方法创建未保存的实例。

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

请参阅入门文件中的“使用工厂”。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文