我正在测试Ruby Rails网站,并希望开始进行单元和功能测试。
我正在测试Ruby Rails网站,并希望开始进行单元和功能测试。
黄瓜 和 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
它对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!"
黄瓜 和 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
它对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!"
我的建议(严重)只是绕过内置的轨道单元/功能测试的东西,并直接进行 RSpec的。
内置的rails东西使用了 Test::Unit
与ruby一起提供的框架,它或多或少是JUnit / NUnit / AnyOtherUnit的直接端口。
我发现这些框架都相当乏味和烦人,导致对编写单元测试的普遍冷漠,这显然不是你想要在这里实现的。
RSpec是一个不同的野兽,以描述你的代码为中心 应该 做,而不是断言它是什么 已经做到了。它将改变您查看测试的方式,并且您将会有更多的乐趣。
如果我听起来像一个粉丝,那只是因为我真的相信RSpec是那么好。我从厌倦了单位/功能测试变成了一个坚定的信徒,几乎完全是因为rspec。
听起来你已经编写了你的应用程序,所以我不确定你会从使用中获得巨额奖金 RSpec
过度 Test::Unit
。
无论如何,无论你选择哪一个,你都会很快遇到另一个问题:管理灯具和模拟(即你的测试“数据”)。所以看一看 早该 和 工厂女工。
您还可以使用Firefox插件测试Web界面,例如 http://selenium.openqa.org/
它将记录点击和文本输入,然后播放并检查页面是否有正确的显示元素。
即使已经编写了应用程序,我也建议使用RSpec over Test :: Unit,因为没有任何应用程序可以完成。您将要添加功能和重构代码。尽早获得正确的测试习惯将有助于减少这些改变