Rails:良好的Rspec2示例用法? (另:黄瓜,腌菜,水豚) [英] Rails: Good Rspec2 example usage? (Also: Cucumber, Pickle, Capybara)

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

问题描述

我在寻找一个最近使用Rspec 2作为测试库的开源应用程序。
我想看看一个有经验的开发人员如何正确地使用库来测试整个堆栈,因为我不断有关于我自己的知识(来自testunit,部分是由于相当稀疏的文档的最新



如果一个项目使用Cucumber,Pickle和/或Capybara以及Rspec 2,你会有我



任何指针?



干杯!

解决方案

我的2美分:



使用Steak代替Cucumber。它是RSpec的核心,它很简单,它做的工作。



https://github.com/cavalle/steak



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



https://github.com/jeffkreeftmeijer/swinger



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



a href =https://github.com/Nerian/akephalos2 =noreferrer> https://github.com/Nerian/akephalos2



RSpec的一个好办法是使用Context。问我是否需要澄清。此外,请注意 let 方法。它返回块返回的任何值。它是有用的声明嘲笑一个对象里面和使用它们的样品。

 功能课程do 

let(:school){School.make!}

上下文登录do
before(:each)do
switch_to_subdomain(school)
end

上下文形式do
before(:each)do
click_link(Courses)
click_link(New course)
end

课程do
end

情景不接受没有姓名的课程do
end

情景不应创建新课程如果在同一所学校有另一个同名的同学do
end
end
end
end

此外,这本书:Pragmatic Programmers的RSpec Book是一本很好的资源,用于启发自己关于RSpec,Capybara,Cucumber和所有这些行为驱动开发的核心概念敏捷事物:)



编辑:



此外,我使用Machinist2作为灯具。
https://github.com/notahat/machinist



效果很好。



还有制造商,它有一个很好的网站和一个非常有用的DSL。



https://github.com/paulelliott/fabrication



您可以使用Machinist with 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 /environment.rb')
require File.expand_path('spec / support / blueprints.rb')
drop
puts填充数据库
1.times do |
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
/ pre>

RSpec 2的文档有很多示例:



http://relishapp.com/rspec



此外,本文还提供了许多其他提示: / p>

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/


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).

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

Any pointers?

Cheers!

解决方案

My 2 cents:

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

https://github.com/cavalle/steak

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

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

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   

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 :)

Edit:

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

Works great. Better than Factory girl.

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

https://github.com/paulelliott/fabrication

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

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

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/

Optimising the execution time of tests:

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示例用法? (另:黄瓜,腌菜,水豚)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆