1. 30
    Rustlings tests3: Writing tests for a function
    1m 2s

Rustlings tests3: Writing tests for a function

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

README for this exercise.

Chris Biscardi: [0:00] In tests3, we have a public function, is_even, that takes a number that's assigned 32 bit integer and returns a Boolean. We're checking to see if the number is divisible by two.

[0:12] At first, we have to assert! That is_even successfully passes through an even value. We'll create our test named is_false_when_odd() by creating another function. We'll use the assert! Macro, just like our first tested, and we'll pass is_even(5). If this passes and we've successfully written a new test.

[0:32] Note that even though it looks like our test has run, it actually hasn't. If we test is_false_when_odd(), and we assert! (is_even(4)), we can see that it didn't work. This is because we haven't specified yet that this is a test.

[0:45] Note that now when we pass five to is_even, our test is failing, whereas before it wasn't because it wasn't running. In this case, we can use the assert_eq! Macro with a value of false and the result of is_even with the argument five. If we change it to four, we can see that our test is still running and fails.