Tag: 代码清理

如何在rspec示例中存根全局日志记录function

我正在使用一些记录到全局静态日志记录类的代码,例如: GlobalLog.debug(“Some message”) 但是在我的测试中,我不想包含真正的日志,因为它引入了许多不需要的依赖项。 所以我想嘲笑它: describe “some function” do before(:all) do log = double(‘log’) GlobalLog = log log.stub(:debug) end … end 不幸的是,因为在每个示例之后清除了双精度数,所以不允许这样做: https://www.relishapp.com/rspec/rspec-mocks/docs/scope 如果我将before(:all)更改为before(:each) ,代码可以正常工作,但是我收到警告: warning: already initialized constant GlobalLog 这会堵塞我的测试输出,所以我想避免警告。 有清洁的解决方案吗?

Rubywarrior Level 4(清理我的代码帮助)

我正在通过Ruby学习编程,我从Railscasts的Ryan Bates那里找到了令人敬畏的Rubywarrior。 不幸的是,我被困在我的代码中抛出了语法错误消息(意外的$ end)。 我不是要求答案,我想自己解决这个问题,但如果有人能够指出我的代码在哪里得到错误,那将是超级的。 谢谢! class Player def initialize @maxhealth = 20 @dying = 7 @previoushealth = @maxhealth @health = warrior.health @warrior = warrior end def play_turn(warrior) # If there are no enemies, rest until health is 100% turn_start_check(warrior) actions(warrior) turn_end_check(warrior) end def actions(warrior) if @damaged_since_last_turn warrior.shoot! elsif @health < @maxhealth warrior.rest! else warrior.walk! […]