Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing test cases(Rspec) for existing application

Being Rspec noob, i dont know this question make sense. BUt i am really confused and couldnt get solution for this.

We have one new requirement, asked us to write Rspec test cases for existing project. They have application with rails3 that already working but they want rspec cases to be written now

My question is

Writing Rspec cases for existing application does make sense?

Can we write spec for already working project

Should we follow any guidelines to write specs for already written app

Thank you

like image 459
devudilip Avatar asked Nov 06 '12 06:11

devudilip


People also ask

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

What type of test is not typical in a rails application?

Don't write route tests, except if you're writing an API, for the endpoints not already covered by integration tests. Don't write view tests. You should be able to change copy or HTML classes without breaking your tests. Just assess critical view elements as part of your in-browser integration tests.

Is RSpec TDD or BDD?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


2 Answers

As dpassage rightly pointed, your question has nothing to do with rspec. It's about unit testing a legacy code.

Writing Rspec cases for existing application does make sense? Can we write spec for already working project

Yes. It's best when you write your tests first before you write the application. It's never too late to write tests, starting late does not hurt. Testing legacy code: Adding unit tests to legacy code

Should we follow any guidelines to write specs for already written app

My two cents on this:

  • Add tests incrementally

  • Test first the most important parts of your application. i.e. Start writing the unit tests first and focus on important models (instead of all models). Once you test your models move to functional tests(controllers) and integration tests. Remember: To eat an elephant we need to eat one piece at a time.

  • If your existing models are difficult to write test, it implies that you need to refactor your code. Refactor to make your models simple to test.

  • Always write unit tests for new models/implementation from NOW.

Useful references:

  • TDD by example - Kent beck book
  • Effective testing: http://www.confreaks.com/videos/917-railsconf2012-testing-best-practices-or-the-five-habits-of-highly-effective-tests
like image 56
18bytes Avatar answered Sep 21 '22 04:09

18bytes


It absolutely makes sense to add test cases to existing code; it will help keep the application working as future enhancements and bug fixes are made.

There's nothing about rspec that makes this different in particular; it's just a testing framework, enabling you to test your code many different ways.

There's a fair amount of literature on writing tests for legacy code; in combination with good documentation such as The Rspec Book, you should be able to get started.

like image 25
dpassage Avatar answered Sep 18 '22 04:09

dpassage