Wednesday, 29 May 2019

Functional testing with Rails 5

I have not blogged here for a couple of years, mostly because my blogging was part of the learning process, and I got to a point where I knew enough to get by. I upgraded to Rails 5 a little while ago, somewhat late because I use JRuby, and I waited for them to be compatible. I am not adding two new models/controllers to my project, and found out about functional testing in Rails 5.

There is none.

It has been replaced by integration testing.

Okay, I can see the point. You want to test if a request gives the right response. If the user has to log in first, you do the request first, then do the one you want to test. However...

Updating tests

I have over 800 functional tests in my project. If I do a full upgrade to Rails 5, then I have a huge amount of work modifying each and every test.

And I do mean every test, as the way we make requests has changed fundamentally, so:

get :new

... has become:

get root_path

The parameters are different too, so a simple find/replace is not going to work here.

At the moment, that is not necessary, but functional tests are to be deprecated, so it is either update all 800 or so tests or add another gem to the project.

No assigns

Integration testing is "black box", it gives no way to look inside to see what variables are getting set. This seems like a really bad idea in practice.

How can it possibly be a good thing to stop checking something? Because that is what I will have to do. A fair proportion of those 800 tests check the value of a variable or three. If they stop doing that, then I cannot be as confident that passing the test means the code is actually okay.

Another big problem is that I use assigns as a way to see what is going on when a test fails. With integration testing, I will know it fails, but have no idea why. Again, it feels like Rails is deliberately withholding useful information, just because it is philosophically better to do it this way.

Backwards compatibility

If only! Changes like this make me regret choosing Rails. I doubt I would choose it again, to be honest.

No comments: