使用 RSpec 测试 Rails Metal/Rack? [英] Testing Rails Metal/Rack with RSpec?

查看:53
本文介绍了使用 RSpec 测试 Rails Metal/Rack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say I have a Metal class named Preview. How do I test it with 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>

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

It seems that RSpec doesn't load up the :post method if the test isn't explicitly for a Controller. I've tried specifying :type => :controller to no avail.

推荐答案

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

rspec has integration specs that wrap rails integration tests (which go through rack). Just put that spec in ./spec/integration/preview_spec.rb.

这篇关于使用 RSpec 测试 Rails Metal/Rack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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