Editing
Ruby class 2011 03 29
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Enter Test::Unit... == So we've been running these generators, see, and there have been these files appearing in our test folder. Those are tests. Duh. Let's run them: <pre>rake test</pre> Yay, tests passed! Let's have a look, open test/unit/user_test.rb. <pre>assert true</pre> So those of you who went through Ruby Koans will remember assert. It translates to 'make sure the following thing is true'. So in the case of this test it says 'make sure true is true'. A bit of a low bar for our testing suite, I think. Good for our ego and not much else. So let's delete it and replace it with something a little more meaningful. <pre>test "a new user saves" do assert User.new.save end</pre> And run rake again: <pre>rake test</pre> It fails! The user did not save. Take a look at app/models/user.rb. The user didn't save because of our validations. Which is good. Our test should look something more like this: <pre>test "a new user should not save" do assert !User.new.save end</pre> and run our tests again...pass! Still not very useful to us, but it's a step in the right direction. Now let's add another test to make sure it saves when we have the fields it wants: <pre>test "a user should save with valid attributes" do assert User.new(:password => 'bill', :email => 'bill').save end</pre> Run again and success! Now you know what testing is. Let's take a closer look at the structure of our testing folders. Inside test/ we've got: <pre>fixtures/ functional/ integration/ performance/ unit/ test_helper.rb</pre> We were just working on tests inside the unit/ folder, which, as it turns out, will be where you spend roughly 98% of your time when you're writing tests. Unit tests are the smallest scale of testing, and, in general, they map to your models, hence user_test.rb. The models (and the database behind them) are the foundation of your application. Which makes it far-and-above the most important place in your application to ensure proper functionality. Functional testing is a little higher level. In Rails functional testing maps to your controllers and ensures that you get the response you expect when you hit a certain action. For example, try adding the following to test/functional/sessions_controller_test.rb: <pre>test "invalid user log in should render log in page" do post 'create' assert_template 'sessions/new' end test "valid user log in should redirect to user's tasks" do user = User.new(:password => 'blah', :email => 'blah') user.save post 'create', { :password => 'blah', :email => 'blah' } assert_redirected_to user_tasks_path(user) end</pre> and run. It all passes. Integration testing is one level higher than functional tests. It's about making sure all functionality across controllers works properly. If you get to the point of writing integration tests then you might have too much time on your hands. Unlike the other testing areas, performance testing is not about making sure your application does what you expect, but instead about making sure your application does it in a timely fashion. Again, if you find yourself writing performance tests without good reason, you should probably go back and write a few more unit tests. Which leaves fixtures. Fixtures are sample data that gets added to the database as soon as the tests start running. Say you get tired of typing in task data every time you want to write a new task test. Take a look at test/fixtures/tasks.yml and change it to look like this: <pre>one: title: Do laundry done_date: nil</pre> Now inside test/unit/user_test.rb add the following: <pre>test "task should save with title" do task = tasks(:one) assert task.save end</pre> Run our tests, and it passes. Unfortunately with the user model and our hashing process it's a little more complicated. So that's testing with Test::Unit in a nutshell.
Summary:
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Noisebridge:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Request account
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Dig in!
Noisebridge
- Status: MOVED
- Donate
- ABOUT
- Accessibility
- Vision
- Blog
Manual
MANUAL
Visitors
Participation
Community Standards
Channels
Operations
Events
EVENTS
Guilds
GUILDS
- Meta
- Electronics
- Fabrication
- Games
- Music
- Library
- Neuro
- Philosophy
- Funding
- Art
- Crypto
- Documentation/Wiki
Wiki
Recent Changes
Random Page
Help
Categories
(Edit)
Tools
What links here
Related changes
Special pages
Page information