上面的简短问题,下面有更多详细信息(主要是不必要的,但认为它们可能有用)。我是一个新的防守和相对较新的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页面,但是无法轻易找到任何关于启用我的自定义记录器的任何内容,以便警卫忽略它,是否有人知道如何实现这一目标?
答案 0 :(得分:1)
您需要config/development.rb
中的记录器,这意味着在测试环境中未定义MY_LOGGER
,从而破坏了您的功能,以便规范正确失败。只需将需求移至config/environment.rb
即可在所有环境中使用。