简体   繁体   English

Rspec找不到自定义模块

[英]Rspec cannot find custom module

I try to test devise user authentication, the problem I've done everything according to samples, however the code still doesn't work. 我尝试测试设计用户身份验证,问题我已根据示例完成了所有操作,但代码仍无法正常工作。

spec/support/devise/devise_support.rb 规格/支持/设计/ devise_support.rb

module ValidUserRequestHelper
    def sign_in_as_a_valid_user
        @user ||= Fabricate(:simple_user)
        post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
    end
end

RSpec.configure do |config|
    config.include ValidUserRequestHelper, :type => :request
end

spec/spec_helper.rb 投机/ spec_helper.rb

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

However when I run test, it fails on the calling to `sign_in_as_a_valid_user' 但是,当我运行测试时,它在调用`sign_in_as_a_valid_user'时失败

undefined local variable or method `sign_in_as_a_valid_user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xc57a0c4>

I don't have idea how to debug this. 我不知道如何调试这个。

The test code is 测试代码是

require 'spec_helper'

describe User do

  before do
        sign_in_as_a_valid_user
  end

...

when you write this in your rspec configuration 当你在rspec配置中写这个时

RSpec.configure do |config|
  config.include ValidUserRequestHelper, :type => :request
end

you tell rspec to only include this helper for request spec. 你告诉rspec只包含请求规范的帮助器。 those are typically located in spec/request . 这些通常位于spec/request deriving from the example of your spec that has describe User in it, i assume that you are writing a model spec, typically located in spec/model . 从您的规范中describe User的示例得出,我假设您正在编写模型规范,通常位于spec/model so when running the spec, rspec won't include it for that spec! 因此,在运行规范时,rspec将不包含该规范!

if you just remove the :type => :request it will get included everywhere. 如果你只是删除:type => :request它将包含在任何地方。 keep in mind, that there is usually a good reason for this kind of restrictions. 请记住,这种限制通常有充分的理由。 for example a helper that only works with a fake browser, like it is done in request specs. 例如,仅适用于虚假浏览器的帮助程序,就像在请求规范中完成的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM