本文介绍了Rails:好的 Rspec2 示例用法?(还有:黄瓜、泡菜、水豚)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a rather recent open source application that uses Rspec 2 as test library.I'd like to see how an experienced developer utilizes the library properly to test the full stack, since I'm constantly in doubt concerning my own knowledge (coming from testunit and partly due to the rather sparse documentation of the latest Rspec release, even though it is constantly improved).

如果一个项目将 Cucumber、Pickle 和/或 Capybara 与 Rspec 2 一起使用,我会高兴得跳起来.

If a project would use Cucumber, Pickle and/or Capybara as well together with Rspec 2, you'd have me jumping for joy.

有什么指点吗?

干杯!

推荐答案

My 2 cents:

My 2 cents:

用牛排代替黄瓜.它的核心是 RSpec,它很简单并且可以胜任.

Use Steak instead of Cucumber. It RSpec at its core, it is simple and it does the job.

https://github.com/cavalle/steak

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

Capybara allow you use different drivers. Some drivers support javascript, run with a browser, faster, slower, etc. Use the best Driver for the spec you are testing using Swinger:

https://github.com/jeffkreeftmeijer/swinger

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

I use my own fork of Akephalos – a driver – which is fast, support javascript, UTF-8 (that's what my fork adds) and doesn't need an external browser.

https://github.com/Nerian/akephalos2

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

A good practice for RSpec is to use 'Context'. Ask me if you need clarification. Also, take note of the let method. It returns whatever the block returns. It is useful for declaring mock a object inside and using them on the samples. .

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

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

Also, the book: The RSpec Book, of Pragmatic Programmers is a very good resource for initiating yourself about the core concepts behind RSpec, Capybara, Cucumber and all this Behaviour Driven Development agile thing :)

此外,我将 Machinist2 用于固定装置.https://github.com/notahat/machinist

Also, I use Machinist2 for fixtures.https://github.com/notahat/machinist

效果很好.比工厂女孩好.

Works great. Better than Factory girl.

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

There is also Fabricator, which have an excellent website and a very usable DSL.

https://github.com/paulelliott/fabrication

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

You can use Machinist with Forgery in order to create intelligent data.

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 任务结合起来,以填充您的开发数据库,​​从而以最终用户看到的方式查看应用程序.

You can combine this with a Thor task in order to populate you development database, to see the application as the final user would see it.

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 的文档有很多例子:

The documentation of RSpec 2 has many examples:

http://relishapp.com/rspec

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

Also, this Post give many other tips:

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

另一个有很好建议的帖子:

Another post with very good advise:

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/

这篇关于Rails:好的 Rspec2 示例用法?(还有:黄瓜、泡菜、水豚)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:29