是否可以在Ruby on Rails中调试单元测试?

时间:2022-06-01 20:58:49

So, I just moved to new project that is built on Ruby on Rails. I'm new to it and I'm still learning it. I can debug most of the application code, but it seems I can not debug unit test code and code that is tested.

所以,我刚搬到一个新的项目,它是在Ruby on Rails上建立的。我是新手,我还在学习。我可以调试大部分的应用程序代码,但是我似乎不能调试单元测试代码和测试代码。

When I run unit testing in debug mode, I can set a breakpoint in unit test class code, for example where fixtures are added, but breakpoints do not work inside test method code and do not work inside tested code, where they do work during normal workflow. Everyone else in team uses print statement for unit test debugging.

当我在调试模式下运行单元测试时,我可以在单元测试类代码中设置一个断点,例如添加了fixture,但是断点在测试方法代码中不能工作,在测试代码中也不能工作,在正常的工作流中也不能工作。团队中的其他人都使用print语句进行单元测试调试。

So, is that even possible to debug unit test code in rails?

那么,甚至可以在rails中调试单元测试代码吗?

We are using jruby 1.3.1, rails-2.3.1 and I use IntelliJ IDEA to debug, also I'm ok if I have to switch to eclipse or netbeans.

我们正在使用jruby 1.3.1、rails-2.3.1和IntelliJ IDEA进行调试,如果我必须切换到eclipse或netbeans,我也没问题。

EDIT: actually, "no, it is not possible" would be a helpful answer as well, if you know it is so. At least I would stop wasting time on it

编辑:实际上,“不,这是不可能的”也是一个有用的答案,如果你知道是这样的话。至少我不会再在上面浪费时间了

2 个解决方案

#1


24  

Yes, it is possible.

是的,这是可能的。

First of all you must install ruby-debug.

首先,必须安装ruby-debug。

sudo gem install ruby-debug

sudo gem安装ruby-debug

then you write

然后你写

debugger

调试器

immediately before the line you wish to start debugging.

在您希望开始调试的行之前。

Next step: run your unit tests as you would do normaly.

下一步:像平常一样运行单元测试。

At the moment that ruby reaches the line that contain the debugger directive it will stop and show you a console prompt.

当ruby到达包含调试器指令的行时,它将停止并显示控制台提示符。

Now you can debug in the same way as you would do with gdb.

现在可以用与gdb相同的方式进行调试。

This link explains it in a very nice way: http://blog.nanorails.com/articles/2006/07/14/a-better-rails-debugger-ruby-debug

这个链接以一种非常好的方式解释了这一点:http://blog.nanorails.com/articles/2006/07/14/awell -rails-debug -debug。

I hope this informtaion will be useful to you.

我希望这个信息对你有用。

#2


3  

As a (late) update, you could also use byebug which is targeting Ruby 2 debugging.

作为(后期)更新,您还可以使用byebug,它的目标是Ruby 2调试。

It's also included by default in the Gemfile that comes with any new Rails project and there is also a nice tutorial in the Debugging Rails Applications Guide.

它还默认包含在Gemfile中,该文件随任何新Rails项目一起提供,在调试Rails应用程序指南中还有一个很好的教程。

You can just insert

你可以插入

byebug

anywhere in your test file and then run the test as you normally would.

在测试文件的任何地方,然后像往常一样运行测试。

#1


24  

Yes, it is possible.

是的,这是可能的。

First of all you must install ruby-debug.

首先,必须安装ruby-debug。

sudo gem install ruby-debug

sudo gem安装ruby-debug

then you write

然后你写

debugger

调试器

immediately before the line you wish to start debugging.

在您希望开始调试的行之前。

Next step: run your unit tests as you would do normaly.

下一步:像平常一样运行单元测试。

At the moment that ruby reaches the line that contain the debugger directive it will stop and show you a console prompt.

当ruby到达包含调试器指令的行时,它将停止并显示控制台提示符。

Now you can debug in the same way as you would do with gdb.

现在可以用与gdb相同的方式进行调试。

This link explains it in a very nice way: http://blog.nanorails.com/articles/2006/07/14/a-better-rails-debugger-ruby-debug

这个链接以一种非常好的方式解释了这一点:http://blog.nanorails.com/articles/2006/07/14/awell -rails-debug -debug。

I hope this informtaion will be useful to you.

我希望这个信息对你有用。

#2


3  

As a (late) update, you could also use byebug which is targeting Ruby 2 debugging.

作为(后期)更新,您还可以使用byebug,它的目标是Ruby 2调试。

It's also included by default in the Gemfile that comes with any new Rails project and there is also a nice tutorial in the Debugging Rails Applications Guide.

它还默认包含在Gemfile中,该文件随任何新Rails项目一起提供,在调试Rails应用程序指南中还有一个很好的教程。

You can just insert

你可以插入

byebug

anywhere in your test file and then run the test as you normally would.

在测试文件的任何地方,然后像往常一样运行测试。