简体   繁体   English

调试Rails测试

[英]Debugging rails tests

Testing has been the thing I've avoided until I realized that with well written tests I can avoid having to enter console and running a string of commands over and over again. 测试一直是我一直避免的事情,直到我意识到编写良好的测试我可以避免不得不进入控制台并一遍又一遍地运行一串命令。 The big issue I'm having, however, is that when a test fails, I can't see much data to use to debug with. 但是,我遇到的最大问题是,当测试失败时,我看不到有多少数据可用于调试。 For example, when I'm testing models I need to see the models attributes, and I need to control when it's been built, created, and saved, and a way to detect these states. 例如,当我测试模型时,我需要查看模型属性,并且需要控制何时构建,创建和保存模型,以及检测这些状态的方法。 With rails fixtures and plain vanilla tests this is annoying to do. 使用导轨固定装置和普通的香草测试,这很烦人。

How can I make debugging fixtures and rails tests easier? 如何使调试夹具和导轨测试变得更容易? Is there a way to display more console-like information when a test fails? 测试失败时,是否可以显示更多类似控制台的信息? Which testing tools can I use to debug more easily? 我可以使用哪些测试工具更轻松地进行调试? After all, debugging the tests themselves is a huge waste of time, and the biggest obstacle to using them in the first place. 毕竟,调试测试本身会浪费大量时间,并且是首先使用它们的最大障碍。

Thanks! 谢谢!

Logging helps a lot to investigate and fix issues. 日志记录有助于调查和解决问题。 Not only in test but also when you deploy it on production. 不仅在测试中,而且还在生产中部署时。 You can log state of your models and other useful info. 您可以记录模型的状态和其他有用的信息。 Also standard rails logging logs SQL that is send to the database so you can easily see what's going on there. 此外,标准的rails日志记录了发送到数据库的SQL,因此您可以轻松查看那里发生了什么。 SQL logging is on by default in dev and test and looks similar to this: 在开发和测试中,默认情况下SQL日志记录处于打开状态,其外观类似于以下内容:

[2015-09-24 23:19:36 ledger DEBUG]:   [1m[36m (0.2ms)[0m  [1mBEGIN[0m
[2015-09-24 23:19:36 ledger DEBUG]:   [1m[35mSQL (2.1ms)[0m  INSERT INTO "projections_tags" ("ledger_id", "tag_id", "name", "authorized_user_ids") VALUES ($1, $2, $3, $4) RETURNING "id"  [["ledger_id", "ea9a68c5-c7c8-4964-b078-45bfc93d41ef"], ["tag_id", 9], ["name", "test"], ["authorized_user_ids", "{1}"]]
[2015-09-24 23:19:36 ledger DEBUG]:   [1m[36m (0.4ms)[0m  [1mCOMMIT[0m

I'm using log4r as a logging library since it gives more control over the log output. 我将log4r用作日志记录库,因为它可以更好地控制日志输出。

I also don't like debugging but in some cases it really helps. 我也不喜欢调试,但在某些情况下确实有帮助。 In this case byebug is your friend. 在这种情况下, byebug是您的朋友。

And of corse rails console and rails dbconsole 和科西斯rails consolerails dbconsole

Debugging your tests is not very different from debugging your code. 调试测试与调试代码没有太大区别。

Rails already comes with byebug gem (uncomment it, if necessary): Rails已经附带了byebug gem(如果需要,请取消注释):

group :development, :test do
  gem 'byebug'
end

Now you are ready to put 现在您可以放了

debugger

line anywhere in your code. 在代码中的任何地方排行。

Byebug supports many additional features, which you can find here: https://github.com/deivid-rodriguez/byebug Byebug支持许多其他功能,您可以在此处找到: https : //github.com/deivid-rodriguez/byebug

Also check https://github.com/deivid-rodriguez/pry-byebug project which provides even better experience for debugging. 还请检查https://github.com/deivid-rodriguez/pry-byebug项目,该项目可提供更好的调试体验。

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

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