Skip to content

Instantly share code, notes, and snippets.

@randalmaile
Last active December 30, 2015 15:59
Show Gist options
  • Save randalmaile/7851877 to your computer and use it in GitHub Desktop.
Save randalmaile/7851877 to your computer and use it in GitHub Desktop.
Reading RSpec Tests checkpoint
"<a href='http://www.bloc.io'>Bloc</a>"
"<a href='http://www.google.com'>Google</a>"
1. I've installed Pry (as an alternative to IRB). It's pretty cool, but I can't seem to run the describe method. I get: "undefined method `describe' for main:Object" - any ideas - thx!?
2. Is using a REPL - A read–eval–print loop (REPL) tool a better idea than running code on the Bloc site?
3. Am I understanding correctly?:
'''
describe "hello" do
it "says hello to someone" do
hello("Steve", "Jobs").should eq("Hello Steve Jobs.")
end
end
'''
a. *describe* takes a string that = the name of the method your are testing?
b. *it* calls the method in question and validates its return?
c. *it* takes a string that describes the method and resulting output?
d. *.should"* is a method on hello that executes the hello method?
e. *.eq("Output messg") is setting the output (to validate the comparison) to the string passed as a parameter?
def link_to(text, address)
p "<a href='#{address}'>#{text}</a>"
end
describe "link_to" do
it "should return a valid link for Bloc" do
link_to("Bloc", "http://www.bloc.io").should eq("<a href='http://www.bloc.io'>Bloc</a>")
end
it "should return a valid link for Google" do
link_to("Google", "http://www.google.com").should eq("<a href='http://www.google.com'>Google</a>")
end
end
def hello(name)
p "Hello #{name}"
end
@mkwiatkowski
Copy link

First of all, you were not supposed to copy the rspec tests into the code window on Bloc. The way this works is that you should only provide the code and the tests will be run for you when you click "Run". Bloc streamlines the process by connecting those two elements (tests and your code) and making them work. You will later learn how to do that on your own.

  1. You don't run rspec tests the same way you run the rest of the code. I can show you how to run rspec tests on your code separately, but for now please focus on learning Ruby and rely on Bloc for running tests.

  2. It's always good to try out things in irb (Ruby's REPL), but you should go back to Bloc to validate your ideas, because this is where the tests are. What you can do is pasting your code into irb and then manually invoking it. It's useful when you don't understand something and try to debug further. For an example, see https://gist.github.com/mkwiatkowski/7855778

  3. OK, one at a time.

    a. Usually that's the name, but there is no strict rule: it can be anything descriptive.
    b. Well, strictly it doesn't call the method, it just contains code that does it.
    c. Yes, that's correct.
    d. No, should works on hello method's output (return value). hello("Steve", "Jobs") is the actual method call, just like you would do that normally. Once the method returns, should comes in and checks if the condition on the output value is passing.
    e. eq is not strictly "setting" the output. It just checks if the output of the method call is the same as the argument. In this case it checks if the output (i.e. return value) of hello("Steve", "Jobs") is the same as (i.e. equals to) "Hello Steve Jobs.". If it is, the test will move on, if it isn't, an error will be raised: those are the test failures you see.

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