3

假设我有一个名为 Preview 的 Metal 类。如何使用 RSpec 对其进行测试?

当我尝试:

require 'spec_helper'

describe Preview do

  it "should return the posted content" do
    post "/preview", :content => "*title*"
    response.body.should == "*title*"
  end

end

我得到:

undefined method `post' for #<ActiveSupport::TestCase::Subclass_1:0x1058b3098>

:post如果测试不是明确针对控制器,RSpec 似乎不会加载该方法。我试过指定:type => :controller无济于事。

4

3 回答 3

6

rspec 具有包含 rails 集成测试(通过机架)的集成规范。只需将该规范放入./spec/integration/preview_spec.rb.

于 2010-03-16T11:12:30.317 回答
1

我认为最好用 rack/test 测试

于 2010-03-15T20:18:33.313 回答
0

如今(rspec-rails 2.2.0 和 rails 3)您可以使用Request Specs来实现:

根据自述文件

请求规范位于规范/请求中

[...]

请求规范混合了 Rails 集成测试的行为。有关更多信息,请参阅 ActionDispatch::Integration::Runner 的文档。

于 2010-12-01T00:15:39.323 回答