处理旧版 Rails 应用程序

发布于 2024-12-22 08:34:18 字数 622 浏览 5 评论 0原文

我有一个非常旧且写得不好的 Rails 应用程序。大约有9000行代码和零测试。大部分代码都在控制器中,更糟糕的是,有大量的外部 API 调用、对随机脚本的系统调用等。

也没有开发环境,一切都设置为针对生产数据库工作。不仅仅是一个数据库,大约有 10 个不同的数据库,因为该应用程序是大型网站的管理后端。

我的第一个想法是对我将要处理的部分至少获得一定程度的测试覆盖率,但我无法让该东西在生产服务器以外的其他地方工作。

还有大量的旧宝石和已弃用的警告,但在进行测试之前我什至无法考虑升级宝石。重写整个内容不是一个选择,我将不得不很快添加/更改内容。

我不太确定如何测试这样的应用程序,因为有太多东西可能会出错。我真正想做的是编写一些集成测试,然后开始重构,但我无法在生产环境中这样做。

使用一堆存根和模拟编写单元测试似乎没有多大帮助,因为我要处理的代码基本上必须从头开始重写。

我可以采取哪些步骤来在我的开发计算机上基本上复制一个巨大的复杂生产环境,以便我可以独立地完成工作?

编辑:关于该应用程序的一些有趣的事实。当我第一次尝试运行它时,它一直冻结,没有任何错误消息......直到大约半小时后我发现,连接到数据库(不可用)的超时设置为90分钟

I have a very old and poorly written Rails app. There are about 9000 lines of code and zero tests. Most of the code is in controllers, and even worse, there are tons of external API calls, system calls to random scripts, etc.

There is also no development environment, everything is set up to work against the production databases. Well not just one database, there are about 10 different databases, since the app is kind of admin backend for a larger site.

My first idea was to get at least somewhat decent test coverage around the parts that I'm going to work on, but I'm unable to get the thing working anywhere else than on the production servers.

Also there are tons of old gems and deprecated warnings, but I can't even think about upgrading the gems until there are tests. Rewrite of the whole up is not an option, and I'm going to have to add/change stuff soon.

I'm not really sure how to approach testing such an app, since there's just so much stuff that can go wrong. What I'd really like to do, is to write some integration tests and then start refactoring, but I can't do that on a production environment.

Writing unit tests with bunch of stubs and mocks doesn't seem that helpful, since the code I'm going to work on basically has to be rewritten from scratch.

What are some steps I can take to basically duplicate a hugeass complex production environment on my development machine, so I can do stuff in isolation?

edit: A little fun fact about the app. When I tried to run it for the first time, it kept on freezing without any error messages ... until about a half hour later I found out, that the timeout to connecting to a database (which wasn't available) was set to 90 minutes!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱桃奶球 2024-12-29 08:34:18

所以这是一个相对较新/罕见的情况,因为 Rails“LEGACY”应用程序还不太流行(仍然很年轻)。

然而,我遇到过一些关于如何测试(未经测试的)遗留 Rails 应用程序的不同著作。我推荐的一些内容是:

  1. 此幻灯片
  2. Rails 测试规定" 来自 Pragmatic Programmers。

最终也是最重要的事情是让某种测试工具发挥作用。这意味着让工厂正常工作,让测试数据库正常工作,让 rake 运行(即使这意味着剔除损坏的测试)。从那时起,您可以根据需要返回并测试模块,并确保您正在测试添加到项目中的所有新代码。

这是一项非常痛苦的任务,我对你以正确的方式完成它表示赞赏。

干杯!

So this is a relatively new / rare occurrence, because rails "LEGACY" applications are not too popular yet (still pretty young).

However, I have come across a few different writings about how to test (untested) legacy rails applications. A few that I recommend are:

  1. This slideshare
  2. Chapter 18 of "Rails Test Presciptions" from Pragmatic Programmers.

The ultimate and most important thing is to get SOME sort of a test harness working. This means getting factories working, getting your test database working, getting rake running (even if that means stripping out broken tests). From that point, you go back and test modules as you need them, as well as MAKING SURE you are testing all new pieces of code you add to the project.

This is a hugely painful task, and I applaud you for looking to do it the right way.

Cheers!

浊酒尽余欢 2024-12-29 08:34:18

我参与过几次救援任务,大部分是非常糟糕的 PHP 项目,但不仅如此 - 80% 糟糕的程序员留下的绝望的产品所有者付出了很大的代价来完成他们放弃的项目:)。

  • 我的方法是使用蛮力。
  • 开始开发并修复问题,直到它运行起来,通过纯粹的力量 - 修复,尝试,修复,尝试。
  • 您最终将会让它发挥作用。
  • 9000 行代码并不是太多代码,我想说对于糟糕的遗留代码来说这是合理的大小,它可能会更糟。
  • 一点一点开始慢慢重构。
  • 这些任务需要时间,所以不要指望它会立即发生。

I worked on several rescue missions, mostly PHP projects that were really bad, but not only - desperate product owners left by bad 80% programmers pay nice to finish their abandoned projects :) .

  • My approach is to use brute force.
  • Start it in development and fix things until you get it running, by means of sheer power - fix, try, fix, try.
  • You will get it working eventually.
  • 9000 lines of code is not much code, I'd say it's reasonable size for bad legacy code, it could had been a lot worse.
  • Start refactoring slowly bit by bit.
  • These task will need time, so don't expect it to happen at once.
翻身的咸鱼 2024-12-29 08:34:18

欢迎乘坐这种可怕的船。

上次发生在我身上时,我用 Capybara 编写了许多测试,并且:

  • 我检查了预期在数据库中创建或不创建的内容...

  • ...然后一点一点,我正在重构

在这种情况下,除了接近应用程序之外,我没有看到任何其他方法在最高级别并在最低级别检查结果,没有任何存根/模拟。

Welcome on this kind of horrible boat.

Last time it happened to me, I wrote numbers of tests with Capybara and:

  • I checked what was expected to be created or not in db...

  • ... then bit after bit, I'm refactoring

In this case, I don't see any other means than approaching the app at the highest level and check the result at the lowest, without any stub/mock.

七堇年 2024-12-29 08:34:18

什么版本

Rails,
Ruby,
Mysql socket,

它使用

?我必须开发这样的应用程序。这是我必须做的:

Copy DB over, 
do not run the migrations, 
installed gems as production environment, 
do not update gems. 

我的应用程序使用了一个非常具体的 gem 版本(gettext v=1.10)

What version of

Rails,
Ruby,
Mysql socket,

is it using?

I had to work on a app like this. Here is what I had to do:

Copy DB over, 
do not run the migrations, 
installed gems as production environment, 
do not update gems. 

My app used a very specific gem version (gettext v=1.10)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文