如何让黄瓜和泡菜与 mongo_mapper、machinist 和 machinist_mongo 一起工作?

发布于 2024-09-09 22:24:46 字数 2024 浏览 8 评论 0原文

我想要机械师machinist_mongo, mongo_mapper黄瓜pickle 一起玩得很好。

目前,我的机械师配置了所有蓝图,并且正在使用 Cucumber 来执行 BDD。到目前为止,一切都很好。我的问题是我必须为我的所有机械师蓝图编写自定义黄瓜步骤。这本身并不是一个真正的问题,因为它并没有阻止我继续前进,但作为一名检查 Rails 的 .NET 开发人员,必须为每个蓝图编写一个步骤感觉真的很脏,而在 .NET 中我可能可以使用反射。

有什么方法可以让pickle内置在capture_modelcapture_plural_factory等中来识别我的机械师蓝图吗?

我非常有信心我已经正确配置和设置了机械师,因为当我在自定义黄瓜步骤中使用 blueprintname.make 时,一切正常。

宝石版本:
导轨2.3.8
黄瓜0.8.3
黄瓜导轨 0.3.2
蒙戈1.0.5
mongo_mapper 0.8.2
泡菜0.3.0
机械师1.0.6
machinist_mongo 1.1.1


features/support/pickle.rb:
require 'pickle/world'
Pickle.configure do |config|
  config.adapters = [:machinist]
end

我尝试使用 config.adapters = [:machinist, Machinist::MongoMapperAdapter] 但收到一条错误,指出 没有方法 factories机械师::MongoMapperAdapter

Machinist::MongoMapperAdapter:Class 的未定义方法“工厂”(NoMethodError) /usr/local/lib/ruby/gems/1.8/gems/pickle-0.3.0/lib/pickle/config.rb:25:in “工厂”

features/support/machinist.rb:
require 'machinist'
require 'machinist/mongo_mapper'
require "#{Rails.root}/spec/blueprints"
require 'database_cleaner'
Before { Sham.reset } # reset Shams in between scenarios
spec/blueprints.rb (truncated for clarity)
require 'sham'
require 'faker'

Sham.code { Faker::Lorem.words 1 }

AccessCode.blueprint do
  code
end
app/models/access_code.rb
class AccessCode
  include MongoMapper::Document

  key :code, String, :required => true
end

I would like to get machinist, machinist_mongo, mongo_mapper, cucumber and pickle to play nice together.

Currently I have machinist with all my blueprints configured and am using cucumber to do BDD. So far so good. My problem is I am having to write custom cucumber steps for all of my machinist blueprints. It is not really a problem per se, since it is not stopping me in my tracks, but as a .NET dev checking out rails, it feels really dirty to have to write a step for each blueprint whereas in .NET I could probably use reflection.

Is there any way I can get pickle's built in capture_model, capture_plural_factory, etc, to recognize my machinist blueprints?

I am pretty confident I have machinist configured and set up correctly, because when I use blueprintname.make, in a custom cucumber step, everything works out correctly.

Gem versions:
rails 2.3.8
cucumber 0.8.3
cucumber-rails 0.3.2
mongo 1.0.5
mongo_mapper 0.8.2
pickle 0.3.0
machinist 1.0.6
machinist_mongo 1.1.1


features/support/pickle.rb:

require 'pickle/world'
Pickle.configure do |config|
  config.adapters = [:machinist]
end

I tried using config.adapters = [:machinist, Machinist::MongoMapperAdapter] but I get an error stating that there is no method factories for Machinist::MongoMapperAdapter.

undefined method `factories' for Machinist::MongoMapperAdapter:Class (NoMethodError) /usr/local/lib/ruby/gems/1.8/gems/pickle-0.3.0/lib/pickle/config.rb:25:in `factories'

features/support/machinist.rb:

require 'machinist'
require 'machinist/mongo_mapper'
require "#{Rails.root}/spec/blueprints"
require 'database_cleaner'
Before { Sham.reset } # reset Shams in between scenarios

spec/blueprints.rb (truncated for clarity)

require 'sham'
require 'faker'

Sham.code { Faker::Lorem.words 1 }

AccessCode.blueprint do
  code
end

app/models/access_code.rb

class AccessCode
  include MongoMapper::Document

  key :code, String, :required => true
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

思念满溢 2024-09-16 22:24:46

经过几天的头撞墙之后,我的一切基本上都在工作(我说大部分工作是因为我不确定是否有什么问题我还没有发现)。一旦我弄清楚了,修复实际上非常简单。

要解决此问题,请让我的 cucumber 步骤与 pickle,我更改了 MongoMapper::Document 以包含 Pickle::Adapter::Base。我使用了 附带的 lib/pickle/adapters/active_record.rb 和 data_mapper.rb (与 active_record.rb 相同的路径)以泡菜为例。我仍然需要 machinist_mongo,大概是为了将 pickle 连接到我的机械师蓝图。

我不能将 def self.model_classes 中的代码归功于它 - 它是从 tjtuom 的 pickle 叉

附言。如果这是完全错误的做法,请随时批评或提出建议,我是一个十足的 ruby​​ 菜鸟。

module MongoMapper::Document
  module PickleAdapter
    include Pickle::Adapter::Base

    def self.model_classes
      @@model_classes ||= ::MongoMapper::Document.descendants.to_a +
        ::MongoMapper::Document.descendants.map { |klass| klass.subclasses }.flatten
    end

    # get a list of column names for a given class
    def self.column_names(klass)
      klass.column_names
    end

    # Get an instance by id of the model
    def self.get_model(klass, id)
      klass.find(id)
    end

    # Find the first instance matching conditions
    def self.find_first_model(klass, conditions)
      klass.find(:first, :conditions => conditions)
    end

    # Find all models matching conditions
    def self.find_all_models(klass, conditions)
      klass.find(:all, :conditions => conditions)
    end
  end
end

为机械师设置pickle:

Pickle.configure do |config|
  config.adapters = [:machinist]
end

为mongo配置database_cleaner

require 'database_cleaner'
require 'database_cleaner/cucumber'

DatabaseCleaner.orm = 'mongo_mapper'
DatabaseCleaner.strategy = :truncation

After days of beating my head against the wall, I have everything mostly working (I say mostly working because I'm not sure if there is something wrong that I haven't discovered yet). The fix was actually pretty simple once I figured it out.

To resolve the issue, and get my cucumber steps working with pickle, I changed MongoMapper::Document to include Pickle::Adapter::Base. I used lib/pickle/adapters/active_record.rb and data_mapper.rb (same path as active_record.rb) that come with pickle as an example. I did still need machinist_mongo, presumably to hook up pickle to my machinist blueprints.

I can't take credit for the code in def self.model_classes - it is stolen from tjtuom's pickle fork.

PS. If this is the completely wrong way to do it, please feel free to criticize or give suggestions, I am a complete ruby noob.

module MongoMapper::Document
  module PickleAdapter
    include Pickle::Adapter::Base

    def self.model_classes
      @@model_classes ||= ::MongoMapper::Document.descendants.to_a +
        ::MongoMapper::Document.descendants.map { |klass| klass.subclasses }.flatten
    end

    # get a list of column names for a given class
    def self.column_names(klass)
      klass.column_names
    end

    # Get an instance by id of the model
    def self.get_model(klass, id)
      klass.find(id)
    end

    # Find the first instance matching conditions
    def self.find_first_model(klass, conditions)
      klass.find(:first, :conditions => conditions)
    end

    # Find all models matching conditions
    def self.find_all_models(klass, conditions)
      klass.find(:all, :conditions => conditions)
    end
  end
end

Set up pickle for machinist:

Pickle.configure do |config|
  config.adapters = [:machinist]
end

To configure database_cleaner for mongo:

require 'database_cleaner'
require 'database_cleaner/cucumber'

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