Rails:好的 Rspec2 示例用法吗? (还有:黄瓜、泡菜、水豚)

发布于 2024-10-11 01:52:02 字数 1536 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2024-10-18 01:52:02

我的两分钱:

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

https://github.com/cavalle/steak

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

https://github.com/jeffkreeftmeijer/swinger

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

https://github.com/Nerian/akephalos2

RSpec 的一个好习惯是使用“Context”。问我是否需要澄清。另外,请注意 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   

另外,Pragmatic Programmers 的《The 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

您可以将其与 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/< /a>

优化测试的执行时间:

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

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文