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 3 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.

egghead
egghead
~ 10 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today