1

当我尝试运行我的请求规范时收到以下错误:

POST :: /users/:id/authentications request::successful request#test_0001_Adds an authentication record to a user:
NoMethodError: undefined method `post' for #<#<Class:0x007fa607163028>:0x007fa6070012c0>
  test/requests/authentications_test.rb:9:in `block (3 levels) in <main>'

这是测试本身:

require "minitest_helper"

describe "POST :: /users/:id/authentications request" do
  describe "successful request" do
    it "Adds an authentication record to a user" do
      user = create_user

      post user_authentications_path(user)

      response.status.must_equal "200"
    end
  end
end

这是minitest_helper.rb文件:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/autorun"
require "minitest/rails"
require "minitest/rails/capybara"

Dir[Rails.root.join("test/support/**/*.rb")].each {|f| require f}

class ActiveSupport::TestCase
end

# database cleaner
DatabaseCleaner.strategy = :transaction

class MiniTest::Spec
  before :each do
    DatabaseCleaner.start
  end

  after :each do
    DatabaseCleaner.clean
  end
end

class RequestTest < MiniTest::Spec
  include Rails.application.routes.url_helpers

  register_spec_type(/request$/, self)
end

事物的相关版本: Rails:3.2.13 minitest-rails:0.9.2 minitest-rails-capybara:0.9.0

我不能打电话真的没有意义post。看起来网络上的所有其他示例都可以做到这一点。

对此的任何帮助将不胜感激。

4

1 回答 1

2

你的测试助手有很多事情要做。您似乎已经复制了几种不同的方法来在您的 rails 测试中运行 minitest。我建议如下:

从您的测试助手中删除以下内容:

# database cleaner
DatabaseCleaner.strategy = :transaction

class MiniTest::Spec
  before :each do
    DatabaseCleaner.start
  end

  after :each do
    DatabaseCleaner.clean
  end
end

class RequestTest < MiniTest::Spec
  include Rails.application.routes.url_helpers

  register_spec_type(/request$/, self)
end

将以下内容添加到您的测试助手中:

class ActionDispatch::IntegrationTest
  # Register "request" tests to be handled by IntegrationTest
  register_spec_type(/Request( ?Test)?\z/i, self)
end
于 2013-05-19T18:35:43.277 回答