Skip to content
Katrin edited this page Dec 22, 2015 · 1 revision

#Testing to get feedback on the functionality of your code.

  • MiniTest: a testing framework built in Ruby.

  • Rspec: separate gem -> test library you have to import it.

Unit Test: from the Java world it means you test one behavioral unit from the application. A unit is a conceptual thing. For example “Event” It could be a model, a view or controller. The unit test only tests what that unit does , not the interaction with the system (only one assert) You can write unit tests for models and sometimes for controllers*. Tests are useful because if something breaks you know it, confidence because you understand what needs to be done and how it is done, confident to make changes (if you break sth you’ll know about it. Technical & Editorial Reasons (Editorial Reasons: How does it work). TDD - Test Driven Development : Approach to use the tests as a design tool to make better decisions.

When a project gets more complicated, and you can’t remember the interactions and all the bits - It is a good point to start testing .

Joe created some tests

3 different tests in 3 different formats

tests look like method you name them

tests/models/event_test.rb

3 formats but they do the same thing.

Normally you run the tests before deploying.

We looked at the 3 model tests created by Joe and decided to work on the branch Tests

We completed the third format with the alternate behavior - assert -f>alse

running rake test in the command line will run the tests in rails

Assert only considers the first argument

assert_equal ensures that expected == actual is true - > explicitly says I expect this to be that.

The method can return “True” or “False” - so in the tests we have covered the method completely (nil is falsey in Ruby)

Looked what happens when it works and when it doesn’t work.

test.helper -> sets up the basics for the tests

*controller tests normally test more complicated because of interactions , they deal with several things

acceptance tests: how the user interacts with the app. You talk about features and scenarios. We began writing a test for “validating organizer_email”

Convention

instance method hash method e.g: #open? class method .method

method .valid? see -> http://guides.rubyonrails.org/active_record_validations.html#working-with-validation-errors

event.valid? -> will return true or false

We want an empty array back because that means no errors.

Good Idea: to see them fail at least once, to corroborate that we are testing what we think we are testing.

When the test is not very descriptive, we can write a helper method. (assert_attribute_valid)

put the methods inside a module so they could be accessed

method admin_login is not an assertion should be placed somewhere else