3.2
Test Examples
Let’s take for example the case of handling apassword input field when a user registers with ourapplication. There are a variety of scenarios that wewould want to look out for:
•
Is the password more than 4 characters?
•
Is the field Nil? (Rubys equivalent to Null)
•
Is the password the same as their username?These are pretty straight forward requirements andbefore we begin we can develop our test cases for them.
def test_these_passwordsassert !User.new("name","abc").is_password_safe?assert !User.new("name","").is_password_safe?assert !User.new("name","name").is_password_safe?assert User.new("name","JiEhf").is_password_safe?end
Table 4 - Example method containing test cases
Notice the beginning of the method is “test”. Thelayout the test file is such that any method beginningwith “test” as the first four letters will be run. You couldinclude as many test methods as you would like inside asingle script and they will each be executed. At thispoint we could then create our is_password_safe?method inside our controller which would test again eachof our cases. Running the test would produce thefollowing output.
Started .Finished in 0.01 seconds.1 tests, 4 assertions, 0 failures, 0 errors
Table 5 - Test output
Because we were able to implement the correctchecks against the password inside our controller, ourfour assertions passed and we could continuedevelopment. If however we forgot to check for apassword that is less than 4 characters we would havereceived a failure with a description containing the errorand the line that it occurred at. At that point we couldreview our code and make the desired changes so ourcode passes our tests.[3]
4
ActiveRecord and Security
One of the key features of RoR is ActiveRecord.ActiveRecord is a layer that sits between your modelsand your database tables. This methodology is referredto as Object-Relational Mapping [ORM]. ActiveRecordeliminates the need to write any raw SQL queries againstyour database. This makes all RoR applicationscompletely database independent. More importantly itmakes it more secure.One of the most exploited features that are difficultto test against in today’s web applications are SQLinjections. A SQL injection is when a user insertsstandard SQL into a web form, when the form issubmitted the user is then returned more data, often datathat shouldn’t be seen by the user. ActiveRecordprotects RoR applications against this attack. Any rawSQL that would need to developed could easily be doneinside a model method, protecting it from public access.This doesn’t mean that RoR applications are 100%protected against SQL injection attacks but it’s one lesssecurity concern.Today’s more popular web development languagessuch as PHP and Java use raw SQL queries for most allapplication that interact with a database. Because of thepotential threat, you are left with having to find a thirdparty testing tool which can test for such threats. This iswhere RoR excels because of its inclusion of testingframeworks. But what the key issue is SQL injectionsare not a concern with a standard RoR application, butadvanced applications do need to beware.[4]
4.1
Testing without ActiveRecord; quickly
Finished in 11.541093 seconds.3001 tests, 5325 assertions, 0 failures, 0 errors
Table 6 – Very fast testing when removingActiveRecord from the picture [5]
At Ruby Hoedown, a recent Ruby focusedconference; the concept of running tests withoutinteracting with ActiveRecord was demonstrated. Thespeaker, Dan Manges, describes how they do theirtesting without hitting their database, the result is ahighly efficient set of tests that can perform very quicklyand without the expense of database transactions.“
Testing the ActiveRecord framework in addition to your business logic is unnecessary - ActiveRecord has its ownsuite of tests. Therefore, disconnecting from the databaseallows you to test the business logic in isolation from the
Add a Comment
George Ulmerleft a comment
sathiya_rockstarleft a comment