Rails:良好的Rspec2示例用法?(另外:Cucumber,Pickle,Capybara)

pol*_*lau 25 ruby testing rspec cucumber rspec2

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

如果一个项目会使用Cucumber,Pickle和/或Capybara以及Rspec 2,那么你会让我欢欣鼓舞.

有什么指针吗?

干杯!

Ner*_*ian 54

我的2美分:

用牛排代替黄瓜.它是RSpec的核心,它很简单,它可以完成工作.

https://github.com/cavalle/steak

Capybara允许您使用不同的驱动程序.一些驱动程序支持javascript,使用浏览器运行,更快,更慢等.使用Swinger测试规范的最佳驱动程序:

https://github.com/jeffkreeftmeijer/swinger

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

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   
Run Code Online (Sandbox Code Playgroud)

此外,本书:实用程序员的RSpec Book是一个非常好的资源,用于启动自己关于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
Run Code Online (Sandbox Code Playgroud)

您可以将此与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
Run Code Online (Sandbox Code Playgroud)

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/