Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Created May 1, 2014 13:34
Show Gist options
  • Save jarrettmeyer/bf66de831fa30cb0199b to your computer and use it in GitHub Desktop.
Save jarrettmeyer/bf66de831fa30cb0199b to your computer and use it in GitHub Desktop.
describe User do
# The test suite adds a #stub() method to every object. Since instances of objects
# and class definitions are considered objects in Ruby, the #stub() method gets
# applied just about everywhere. It can be used to stub both instance methods and
# class methods.
it "can have tests that hit a database" do
user = User.find(1) # This test will go to the database
expect(user.username).to eq("admin")
end
it "can have tests that are stubbed" do
User.stub(:find) do |id|
User.new { id: id, username: "user-#{id}" }
end
user = User.find(123)
expect(user.username).to eq("user-123") # This has been stubbed
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment