0

上面的简短问题,下面的更多细节(大部分是不必要的,但认为它们可能有用)。我对守卫有点陌生,对rails也比较陌生,但是我已经建立了一个运行良好的rails项目,并且守卫正常运行(使用rspec)。主要的是,守卫和自定义记录器都可以完美地工作并且符合他们自己的预期,但是记录器在启用时会打破守卫。

详细信息如下:如果我像这样定义自定义记录器:

logfile = File.open("#{Rails.root}/log/my.log", 'a')
logfile.sync=true
MY_LOGGER = Logger.new(logfile)

在诸如“/lib/assets/my_logger.rb”之类的文件中

如果我在我的 config/development.rb 文件中需要它,如下所示:

require "assets/my_logger"

然后我可以在我的任何控制器中使用这个记录器,但是只要我把它放在那里,例如

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_back_or user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
      MY_LOGGER.info "this will break guard tests"
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end

然后我依赖于控制器中此操作的警卫测试将中断:

(上面注释了 MY_LOGGER 行):

16:56:33 - INFO - Running: spec/requests/authentication_pages_spec.rb
Running tests with args ["--drb", "-f", "progress", "-r", "/Users/crashandburn4/.rvm/gems/ruby-2.0.0-p0/gems/guard-rspec-2.5.0/lib/guard/rspec/formatter.rb", "-f", "Guard::RSpec::Formatter", "--failure-exit-code", "2", "spec/requests/authentication_pages_spec.rb"]...
..................

Finished in 1.61 seconds
18 examples, 0 failures

(线到位)

16:56:19 - INFO - Running: spec/requests/authentication_pages_spec.rb
Running tests with args ["--drb", "-f", "progress", "-r", "/Users/crashandburn4/.rvm/gems/ruby-2.0.0-p0/gems/guard-rspec-2.5.0/lib/guard/rspec/formatter.rb", "-f", "Guard::RSpec::Formatter", "--failure-exit-code", "2", "spec/requests/authentication_pages_spec.rb"]...
..FFF.............

Failures:

  1) Authentication signin with invalid information 
     Failure/Error: it { should have_title('Sign in') }
       expected #has_title?("Sign in") to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:20:in `block (4 levels) in <top (required)>'

  2) Authentication signin with invalid information 
     Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
       expected #has_selector?("div.alert.alert-error", {:text=>"Invalid"}) to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:21:in `block (4 levels) in <top (required)>'

  3) Authentication signin with invalid information after visiting another page 
     Failure/Error: before { click_link "Home" }
     Capybara::ElementNotFound:
       Unable to find link "Home"
     # ./spec/requests/authentication_pages_spec.rb:23:in `block (5 levels) in <top (required)>'

Finished in 1.62 seconds
18 examples, 3 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:20 # Authentication signin with invalid information 
rspec ./spec/requests/authentication_pages_spec.rb:21 # Authentication signin with invalid information 
rspec ./spec/requests/authentication_pages_spec.rb:24 # Authentication signin with invalid information after visiting another page

(为不必要的堆栈跟踪道歉)

现在我一直在看守卫 github 页面,但无法轻易找到任何关于启用我的自定义记录器的任何内容,以便被守卫忽略,有谁知道如何实现这一点?

4

1 回答 1

1

您需要 logger in config/development.rb,这意味着MY_LOGGER在测试环境中未定义,破坏了您的功能,以便规范正确失败。只需将要求移至config/environment.rb使其在所有环境中可用。

于 2013-06-21T21:38:23.227 回答