Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I really test controllers?

I'm trying to get the best codecoverage/development time result

Currently I use rspec+shoulda to test my models and rspec+capybara to write my acceptance tests.

I tried writing a controller test for a simple crud but it kinda took too long and I got a confusing test in the end(my bad probably)

What`s the best pratice on controller testing with rspec?

Here is a gist on my test and my controller(one test does not pass yet):

https://gist.github.com/991687 https://gist.github.com/991685

like image 320
Marcello Milhomem Avatar asked May 25 '11 19:05

Marcello Milhomem


People also ask

Do controllers need to be tested?

Controllers have to be tested, this was one of the goals of MVC framework - Make those stuff testable. For small applications that approach works really well. Almost all logic is placed inside controller, everything very nicely tested.

Do we write unit test for controller?

We can write an unit test for this controller method by following steps: Create the test data which is returned when our service method is called. We use a concept called test data builder when we are creating the test data for our test.

How do you write a test case for a spring boot controller?

Unit Tests should be written under the src/test/java directory and classpath resources for writing a test should be placed under the src/test/resources directory. For Writing a Unit Test, we need to add the Spring Boot Starter Test dependency in your build configuration file as shown below.


1 Answers

Maybe not.

Sure you can write tests for your controller. It might help write better controllers. But if the logic in your controllers is simple, as it should be, then your controller tests are not where the battle is won.

Personally I prefer well-tested models and a thorough set of integration (acceptance) tests over controller tests any time.

That said, if you have trouble writing tests for controllers, then by all means do test them. At least until you get the hang of it. Then decide whether you want to continue or not. Same goes for every kind of test: try it until you understand it, decide afterwards.

like image 70
molf Avatar answered Sep 28 '22 06:09

molf