Skip to content

Instantly share code, notes, and snippets.

@stacietaylorcima
Last active January 8, 2018 23:03
Show Gist options
  • Save stacietaylorcima/3dc471a269dd6ad02d0285b693c86d94 to your computer and use it in GitHub Desktop.
Save stacietaylorcima/3dc471a269dd6ad02d0285b693c86d94 to your computer and use it in GitHub Desktop.
How to read RSpec tests & Test Driven Development.

Reading RSpec:

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.

Example of RSpec Testing:

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 and it sections.

    • describe: We declare what we're testing (the greet method) in between describe and do. 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 the it and do.
    • TIP: Think of the describe and it strings as building a sentence - “#greet says hello to someone” which is what you’re expecting the strings to do.
  • Inside the describe and it, 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, and eq 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 and it "blocks" with the keyword end. For now, note that each do here is "closed" with an end.


Test-Driven Development

When tests are written to specify a future code's behavior, it's called "Test-Driven Development", or TDD.


@stacietaylorcima
Copy link
Author

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.

  • Specs:
describe "#random_up_to" do

  it "returns a random number less than or equal to the argument" do
    random_number = random_up_to(10)
    expect(random_number <= 10).to be(true)
  end

  it "will not return a random number greater than the argument" do
    random_number = random_up_to(10)
    expect(random_number > 10).to be(false)
  end

end
  • Method:
def random_up_to(num)
  rand(num)
end

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.

  • Specs:
describe "#link_to" do

  it "returns a valid link for Bloc" do
    expect(link_to("Bloc", "http://www.bloc.io")).to eq("<a href='http://www.bloc.io'>Bloc</a>")
  end

  it "returns a valid link for Google" do
    expect(link_to("Google", "http://www.google.com")).to eq("<a href='http://www.google.com'>Google</a>")
  end

end
  • Method:
def link_to(text, address)
  "<a href='#{address}'>#{text}</a>"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment