【问题标题】:Rails: Good Rspec2 example usage? (Also: Cucumber, Pickle, Capybara) [closed]Rails:好的 Rspec2 示例用法? (还有:黄瓜,泡菜,水豚)[关闭]
【发布时间】:2011-01-06 18:17:32
【问题描述】:

我正在寻找一个使用 Rspec 2 作为测试库的较新的开源应用程序。 我想看看有经验的开发人员如何正确利用该库来测试整个堆栈,因为我一直对自己的知识持怀疑态度(来自 testunit,部分原因是最新 Rspec 版本的文档相当稀疏,甚至虽然它在不断改进)。

如果一个项目同时使用 Cucumber、Pickle 和/或 Capybara 以及 Rspec 2,你会高兴得跳起来。

任何指针?

干杯!

【问题讨论】:

    标签: ruby testing rspec cucumber rspec2


    【解决方案1】:

    我的 2 美分:

    用牛排代替黄瓜。它的核心是 RSpec,它很简单,而且能胜任。

    https://github.com/cavalle/steak

    Capybara 允许您使用不同的驱动程序。一些驱动程序支持 javascript,使用浏览器运行,速度更快,速度更慢等。为您正在使用 Swinger 测试的规范使用最好的驱动程序:

    https://github.com/jeffkreeftmeijer/swinger

    我使用自己的 Akephalos 分支 - 一个驱动程序 - 速度快,支持 javascript、UTF-8(这是我的分支添加的)并且不需要外部浏览器。

    https://github.com/Nerian/akephalos2

    RSpec 的一个好习惯是使用“上下文”。问我是否需要澄清。另外,请注意 let 方法。它返回块返回的任何内容。它对于在内部声明模拟对象并在样本上使用它们很有用。 .

    feature "Course" do
    
      let(:school) {School.make!}
    
      context "Loged in" do
        before(:each) do
          switch_to_subdomain(school)
        end
    
        context "In the new course form" do
          before(:each) do
            click_link("Courses")
            click_link("New course")
          end
    
          scenario "New course" do               
          end
    
          scenario "A Course without name should not be accepted" do
          end
    
          scenario "A new course should not be created if there is another one with the same name in the same school" do
          end
        end
      end  
    end   
    

    另外,本书:The RSpec Book, of Pragmatic Programmers 是一个非常好的资源,可以帮助您了解 RSpec、Capybara、Cucumber 和所有这些行为驱动开发敏捷事物背后的核心概念 :)

    编辑:

    另外,我使用 Machinist2 作为固定装置。 https://github.com/notahat/machinist

    效果很好。比工厂女强。

    还有 Fabricator,它有一个优秀的网站和一个非常有用的 DSL。

    https://github.com/paulelliott/fabrication

    您可以将 Machinist 与 Forgery 结合使用以创建智能数据。

    https://github.com/sevenwire/forgery

     School.blueprint do
        name { "Pablo de olavide"}
     end
    
     Student.blueprint do
        first_name { Forgery::Name.first_name}
        last_name { Forgery::Name.last_name }
        school { School.make! }
     end
    

    您可以将此与 Thor 任务结合使用,以填充您的开发数据库,​​以最终用户看到的方式查看应用程序。

    def populate        
        require File.expand_path('config/environment.rb')
        require File.expand_path('spec/support/blueprints.rb')        
        drop
        puts "populating database"
        1.times do |num|
           school = School.make!
           50.times do
           Student.make!(:school => school)
    
           end                                             
        5.times do        
           Course.make!(:school => school)          
           Professor.make!(:school => school)                
           end            
        end
    end
    

    RSpec 2 的文档有很多例子:

    http://relishapp.com/rspec

    此外,这篇文章还提供了许多其他提示:

    http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

    另一个非常好的建议:

    http://flux88.com/2011/05/dry-up-your-rspec-files-with-subject-let-blocks/

    优化测试的执行时间:

    http://blog.leshill.org/blog/2011/10/23/fast-specs.html

    http://jeffkreeftmeijer.com/2011/spec-helpers-bundler-setup-faster-rails-test-suites/

    【讨论】:

    • 非常感谢您的意见!我目前正在使用 Factory Girl,但我想我可能会在不久的将来尝试一下 Machinist。
    • …我读过关于 Steak 的文章,它确实看起来更接近我自己的“风格”(哈?),但我想在一个完整的项目中使用 Cucumber 一次以获得更好的印象。我也考虑过购买 RSpec 书,谢谢你的提示,我想我可能会去买它!我肯定会尝试“上下文”。再次感谢。 — 尽管如此,任何有一些开放项目的人都可以看看吗?
    • 你看,观看大师们完成的 RSpec2 规范的一个非常好的地方是 RSpec2 git 存储库本身:) Cucumber: github.com/dchelimsky/rspec/tree/master/features 和 RSpec; github.com/dchelimsky/rspec/tree/master/spec
    • 每当您认为有人给出了很好的答案或评论时,请给他投票。谢谢。
    • 对不起@Nerian,我的声誉仍然很低,无法给你投票。我会尽快这样做。现在买了 rspec 书。看起来很棒。不过,仍在寻找更复杂的真实世界示例。再次感谢!
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多