找不到Rails MiniTest'post'方法

时间:2013-05-19 03:49:59

标签: ruby-on-rails testing tdd minitest

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

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,这没有任何意义。看起来网络上的其他每个例子都可以做得很好。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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