There are flexible and dynamic ways to test code using test frameworks. One of the most popular frameworks in the Ruby on Rails community is RSpec.
- Important concept: a spec is running the code and testing to find out what happens.
- RSpec runs the code with particular conditions and with particular arguments.
- It sets expectations for the outcome of this test, and the test passes if those expectations are met.
- It's unreasonbale to think a programmer could manually test the code as their programm grows because that would mean testing every method anytime any little thing changes.
- Testing frameworks like RSpec automate the process of testing the code based on expectations set by the programmer.
greet
Method
-
greet
will take 2 arguments, a person's first and last name, and then return a personalized "hello".- As a concrete example, if we call greet with the arguments ("Annie", "Oakley"), it will return "Hello Annie Oakley."
def greet(first, last) "Hello #{first} #{last}." end
-
"Test" our method by using pure Ruby to assert that the return of our method equals what we want it to:
greeting = greet("Annie", "Oakley") greeting == "Hello Annie Oakley." #=> If our method behaves as specified, this would return true. If not, it would return false.
-
In RSpec, the core of the test looks very similar:
greeting = greet("Annie", "Oakley") expect(greeting).to eq("Hello Annie Oakley.")
-
In RSpec, rather than returning true or false, this test will "pass" or "fail" depending on whether the expectation for the method is met. RSpec syntax, however, is more complicated than the example above. Consider the following example:
describe "#greet" do it "says hello to someone" do greeting = greet("Annie", "Oakley") expect(greeting).to eq("Hello Annie Oakley.") end end
-
While the core of the test remains unchanged, we've now wrapped these lines in
describe
andit
sections.describe
: We declare what we're testing (thegreet
method) in betweendescribe
anddo
. The#
before the word "greet" is a convention that indicates the name represents an instance method.it
: We declare our expected behavior in English (for programmers) in between theit
anddo
.- TIP: Think of the
describe
andit
strings as building a sentence -“#greet says hello to someone”
which is what you’re expecting the strings to do.
-
Inside the
describe
andit
, we write the actual test.- It runs our greet method with two arguments, and sets an expectation for what that method will return.
- Use RSpec's
expect
,to
, andeq
methods to assert that we expect the return value of calling greet with the arguments "Annie" and "Oakley" to equal "Hello Annie Oakley."
-
We "close" the
describe
andit
"blocks" with the keyword end. For now, note that eachdo
here is "closed" with an end.
When tests are written to specify a future code's behavior, it's called "Test-Driven Development", or TDD.
Exercises
Random Number Generator:
To complete this exercise, review the test specification below to determine what the name of the method should be and the arguments it needs. Your method will use a random number generator to generate a number, inclusive of argument.
Link Generator:
This exercise will give you a chance to create a method and test it.
Write a method that returns a valid HTML link tag, given the link's text and URL. To complete this exercise, review the test specification below to determine what the name of the method should be, and how the arguments should be ordered. For example, the specification is looking for a method named link_to, so this would be the name of the method. When you pass link_to some "text" and an "address", the method should return a string containing a valid HTML link tag (eg, "Click here!")
Remember that the RSpec test specification below is run automatically when you click the "run" button. It asserts that the code entered in the editor returns the expected result described. There’s no need to copy the RSpec code into the editor.