6

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

Community
  • 1
  • 1
devudilip
  • 1,270
  • 14
  • 25

3 Answers3

10

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:

Community
  • 1
  • 1
18bytes
  • 5,951
  • 7
  • 42
  • 69
7

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.

dpassage
  • 5,423
  • 3
  • 25
  • 53
0

I wrote this gem https://rubygems.org/gems/autogen_rspec.

So this gem has the feature complete but I feel it requires some polishing.

How it works? So after we install this gem and run a server, the gem starts to log all the request, response and assert rspec style. So if you install this on a QA env and as the QA team will test for regression, all the URLs will be logged to a file with responses.

It is up to you to select which test needs to be picked a stored in your test file.

jef
  • 51
  • 1
  • 5