Skip to content

Instantly share code, notes, and snippets.

@jetaggart
Last active December 17, 2015 00:29
Show Gist options
  • Save jetaggart/5521382 to your computer and use it in GitHub Desktop.
Save jetaggart/5521382 to your computer and use it in GitHub Desktop.
class Twitter
def self.find(twitter_username)
# makes an API call to twitter to find the kinsey object!
# returns a TwitterUser object
# TwitterUser.new
end
end
class TwitterUser
def recent_tweets
# calls to twitter to get all of the users recent tweets
# ["#YOLO", "#imakefoodawesome GOod stuff!"]
end
end
class RecentTweets
def initialize(twitter_username)
@twitter_user = Twitter.find(twitter_username)
end
def return_2
tweets = @twitter_user.recent_tweets.take(10)
if tweets < 2
tweets + ["FAKE TWEET: user has less than 2"]
else
tweets
end
end
end
describe RecentTweets
describe "#return_2" do
context "unit level tests, with mocking!" do
before :each do
puts "before each: this runs twice"
@fake_kinsey = double
fake_tweets = [
"#YOLO",
"something happened to me",
"awesome possum"
]
@fake_kinsey.should_receive(:recent_tweets).and_return(fake_tweets)
Twitter.should_receive(:find).with("kinsey").and_return(@fake_kinsey)
end
before :all do
puts "before :all: this runs once"
end
it "returns only 10 tweets" do
recent_tweet_finder = RecentTweets.new("kinsey")
recent_tweet_finder.return_2.should == ["#YOLO", "something happened to me"]
end
context "when there is less than 2" do
it "returns false" do
if @fake_kinsey.nil?
puts "there is no kinsey!"
else
puts "there is a kinsey!"
end
fake_tweets = [
"#YOLO"
]
@fake_kinsey.should_receive(:recent_tweets).and_return(fake_tweets) # change the behavior of kinsey
recent_tweet_finder = RecentTweets.new("kinsey")
recent_tweet_finder.return_2.should == ["#YOLO", "FAKE TWEET: user has less than 2"]
end
end
end
context "Integration test!! No mocking!" do
it "doesn't blow up when I talk to twitter for real" do
if @fake_kinsey.nil?
puts "there is no kinsey!"
else
puts "there is a kinsey!"
end
kinsey = Twitter.find("kinsey")
RecentTweets.new(kinsey).return_2.length.should == 2
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment