如何在Rails 4.2的控制器测试中访问夹具?

时间:2018-09-19 19:29:25

标签: ruby-on-rails unit-testing ruby-on-rails-4

https://guides.rubyonrails.org/v4.2/testing.html#fixtures-are-active-record-objects

  

固定装置是Active Record的实例。如上面第3点所述,您可以直接访问该对象,因为该对象会自动设置为测试用例的局部变量。例如:

     

#这将返回名为david的灯具的User对象。   users(:david)

我有

users_controller_test.rb
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  include Warden::Test::Helpers

  setup do
    # https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
    @user = users(:two)
    login_as(@user, :scope => :user)
  end

  teardown do
    Warden.test_reset!
  end

  test "login as admin" do
    @user = users(:admin)
    login_as(@user, :scope => :user)
    get admin_dashboard_path
    assert_response :success
  end

我要

>rake test
  1) Error:
UsersControllerTest#test_login_as_admin:
NoMethodError: undefined method `users' for #<UsersControllerTest:0x00000009e9a558>
    test/controllers/users_controller_test.rb:8:in `block in <class:UsersControllerTest>'

0 个答案:

没有答案