Testing Rails Metal/Rack with RSpec?

隐身守侯 提交于 2019-12-10 21:54:08

问题


Say I have a Metal class named Preview. How do I test it with RSpec?

When I try:

require 'spec_helper'

describe Preview do

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

end

I get:

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

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.


回答1:


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




回答2:


I think it's better to test with rack/test




回答3:


Nowadays (rspec-rails 2.2.0 and rails 3) you can achieve that using Request Specs:

According to the readme file:

Request specs live in spec/requests

[...]

Request specs mix in behavior from Rails' integration tests. See the docs for ActionDispatch::Integration::Runner for more information.



来源:https://stackoverflow.com/questions/2449614/testing-rails-metal-rack-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!