启动Ruby Rails网站的单元和功能测试的最佳方法是什么?

use*_*883 11 ruby unit-testing ruby-on-rails

我正在测试Ruby Rails网站,并希望开始使用单元和功能测试.

Sam*_*kes 6

黄瓜RSpec值得一看.他们鼓励以行为驱动的,基于示例的风格进行测试.

RSpec是一个用于单元级测试的库:

describe "hello_world"
  it "should say hello to the world" do
    # RSpec comes with its own mock-object framework built in,
    # though it lets you use others if you prefer
    world = mock("World", :population => 6e9)
    world.should_receive(:hello)
    hello_world(world)
  end
end
Run Code Online (Sandbox Code Playgroud)

它对Rails有特殊支持(例如,它可以单独测试模型,视图和控制器),并且可以替换Rails中内置的测试机制.

Cucumber(以前称为RSpec Story Runner)允许您以(相当)简单的英语编写高级验收测试,您可以向客户展示(并同意),然后运行它们:

Story: Commenting on articles

  As a visitor to the blog
  I want to post comments on articles
  So that I can have my 15 minutes of fame

  Scenario: Post a new comment

    Given I am viewing an article
    When I add a comment "Me too!"
    And I fill in the CAPTCHA correctly
    Then I should see a comment "Me too!"
Run Code Online (Sandbox Code Playgroud)