使用Remarkable进行Rails行为驱动开发,提供宏与国际化支持

许多方法都可以开发、测试和集成Rails应用程序:例如使用基础的Test:Unit或ZenTest进行测试驱动开发,利用RSpec、Shoulda或Cucumber进行行为驱动开发。当然也可以编写自定义的RSpec匹配器。

然而,我们却很难找到合适的工具,并挖掘它们的最佳实践。Remarkable试图将这些工具的语法统一起来,并添加更多特性,从而避免Rails行为驱动开发的痛苦。

Remarkable框架使用了自己的DSL(领域特定语言)。它通过提供宏与国际化的支持对RSpec进行了扩展。它提供了一个详尽的RSpec匹配器集合,能够根据各种选项转换所有的ActiveRecord验证。这些选项包括:through, :source, :source_type, :class_name, :foreign_key等。它同时还为ActionController提供了一个匹配器集合。

我们可以使用类似RSpec或Shoulda的语法(来源于Remarkable项目网站):

    1) it { should validate_numericality_of(:age).greater_than(18).only_integer }

    2) it { should validate_numericality_of(:age, :greater_than => 18, :only_integer => true) }


    3) should_validate_numericality_of :age, :greater_than => 18, :only_integer => true

    4) should_validate_numericality_of :age do |m|
         m.only_integer
         m.greater_than 18
         # Or: m.greater_than = 18

       end

这样我们就可以非常容易地编写模型的规格说明:

  describe Post do
    should_belong_to :user
    should_have_many :comments
    should_have_and_belong_to_many :tags


    should_validate_presence_of :body
    should_validate_presence_of :title
    should_validate_uniqueness_of :title, :allow_blank => true
  end

现在可以获取Remarkable 3.0版本,该项目还在持续更新中;下一个版本将提供更多的Rails匹配器,例如对ActionView的支持。

查看英文原文:Rails BDD with Macros, I18n,... with Remarkable

你可能感兴趣的:(使用Remarkable进行Rails行为驱动开发,提供宏与国际化支持)