测试的未定义方法“用户”
undefined method `users' for Tests
我正在尝试编写一个简单的测试,但是当我在我的测试函数中使用它时,我的应用程序没有检测到 'users' 关键字:
post_controller_test.rb
:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
test "should fetch facebook post" do
sign_in users(:one)
get(:save_posts_from_facebook_page,{'id'=>'my_id'},{'access_token'=>Rails.application.secrets.fb_access_token})
assert_response :success
end
end
test_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
end
users.yml
:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
email: hello@testing.com
encrypted_password: <%= Devise.bcrypt(User,'password') %>
links:
causes:
two:
email: MyString
password: MyString
links:
causes:
rake/Error的输出:
Run options: --seed 42684
# Running:
E
Finished in 0.010275s, 97.3279 runs/s, 0.0000 assertions/s.
1) Error:
PostsControllerTest#test_should_fetch_facebook_post:
NoMethodError: undefined method `users' for #<PostsControllerTest:0x000001042152f0>
test/controllers/posts_controller_test.rb:9:in `block in <class:PostsControllerTest>'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
更新:
请注意,我使用的是 MongoId。
当我将 'fixtures :all' 添加到我的 test_helper.rb 时,我得到:
耙子
rake aborted!
NoMethodError: undefined method `fixtures' for ActiveSupport::TestCase:Class
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:7:in `<class:TestCase>'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:5:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/controllers/posts_controller_test.rb:1:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
在您的 test_helper.rb
文件中,您需要添加 fixtures :all
:
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# rest of the codes in this file
end
如上所示在 test_helper.rb
文件中添加该行后,您的测试将通过。
更新
正如 Sergio Tulentsev 在评论中提到的那样,Mongodb 不支持事务,因此在您的案例中不能使用事务固定装置。也看看this answer which states that and also this google group discussion。
我建议您改用 factory_girl,它支持 mongoid,非常棒。
我正在尝试编写一个简单的测试,但是当我在我的测试函数中使用它时,我的应用程序没有检测到 'users' 关键字:
post_controller_test.rb
:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
test "should fetch facebook post" do
sign_in users(:one)
get(:save_posts_from_facebook_page,{'id'=>'my_id'},{'access_token'=>Rails.application.secrets.fb_access_token})
assert_response :success
end
end
test_helper.rb
:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
end
users.yml
:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
email: hello@testing.com
encrypted_password: <%= Devise.bcrypt(User,'password') %>
links:
causes:
two:
email: MyString
password: MyString
links:
causes:
rake/Error的输出:
Run options: --seed 42684
# Running:
E
Finished in 0.010275s, 97.3279 runs/s, 0.0000 assertions/s.
1) Error:
PostsControllerTest#test_should_fetch_facebook_post:
NoMethodError: undefined method `users' for #<PostsControllerTest:0x000001042152f0>
test/controllers/posts_controller_test.rb:9:in `block in <class:PostsControllerTest>'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
更新: 请注意,我使用的是 MongoId。
当我将 'fixtures :all' 添加到我的 test_helper.rb 时,我得到:
耙子
rake aborted!
NoMethodError: undefined method `fixtures' for ActiveSupport::TestCase:Class
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:7:in `<class:TestCase>'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:5:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/controllers/posts_controller_test.rb:1:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
在您的 test_helper.rb
文件中,您需要添加 fixtures :all
:
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# rest of the codes in this file
end
如上所示在 test_helper.rb
文件中添加该行后,您的测试将通过。
更新
正如 Sergio Tulentsev 在评论中提到的那样,Mongodb 不支持事务,因此在您的案例中不能使用事务固定装置。也看看this answer which states that and also this google group discussion。
我建议您改用 factory_girl,它支持 mongoid,非常棒。