Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在不破坏保护测试的情况下定义自定义记录器_Ruby On Rails_Logging_Guard - Fatal编程技术网

Ruby on rails 如何在不破坏保护测试的情况下定义自定义记录器

Ruby on rails 如何在不破坏保护测试的情况下定义自定义记录器,ruby-on-rails,logging,guard,Ruby On Rails,Logging,Guard,上面是一个简短的问题,下面是更多的细节(大部分是不必要的,但认为它们可能有用)。我对guard有点陌生,对rails也比较陌生,但我已经建立了一个运行良好的rails项目,guard运行正常(使用rspec)。最主要的是,guard和定制记录器都能完美地工作,并且正如它们自己所期望的那样,但是记录器在启用时会破坏guard 详细信息如下:如果我这样定义自定义记录器: logfile = File.open("#{Rails.root}/log/my.log", 'a') logfile.sync

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

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

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
(管线就位后)

16:56:19-信息-运行:spec/requests/authentication\u pages\u spec.rb
使用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”、“--失败退出代码”、“规范/请求/验证页面\规范.rb”。。。
…FFF。。。。。。。。。。。。。
失败:
1) 使用无效信息进行身份验证登录
失败/错误:它{应该有_title('Sign-in')}
预期#has#u title?(“登录”)返回true,但返回false
#./spec/requests/authentication\u pages\u spec.rb:20:in'block(4级)in'
2) 使用无效信息进行身份验证登录
失败/错误:它{应该有_选择器('div.alert.alert Error',文本:'Invalid')}
应为#has_selector?(“div.alert.alert error”,{:text=>“Invalid”})返回true,但为false
#./spec/requests/authentication\u pages\u spec.rb:21:in'block(4级)in'
3) 访问其他页面后使用无效信息进行身份验证登录
失败/错误:在{单击链接“主页”}之前
Capybara::ElementNotFound:
找不到“主页”链接
#./spec/requests/authentication\u pages\u spec.rb:23:in'block(5级)in'
以1.62秒完成
18例,3例失败
失败的示例:
rspec./spec/requests/authentication_pages_spec.rb:20#使用无效信息进行身份验证登录
rspec./spec/requests/authentication_pages_spec.rb:21#使用无效信息进行身份验证登录
rspec./spec/requests/authentication_pages_spec.rb:24#访问其他页面后使用无效信息进行身份验证登录
(为不必要的stacktraces道歉)


现在我一直在查看guard github页面,但找不到任何地方,它说明了如何启用自定义记录器,以便guard忽略它,有人知道如何实现这一点吗?

您需要在
config/development.rb
中使用记录器,这意味着,
MY_LOGGER
在测试环境中未定义,破坏了您的功能,因此规范正确失败。只需将require移动到
config/environment.rb
即可在所有环境中使用

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