Skip to content

Instantly share code, notes, and snippets.

@jccarbonfive
Created March 16, 2012 04:39
Show Gist options
  • Select an option

  • Save jccarbonfive/2048534 to your computer and use it in GitHub Desktop.

Select an option

Save jccarbonfive/2048534 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe 'The homepage', :vcr do
before do
visit '/'
end
it 'displays recent posts from Hacker News' do
hacker_news_links = all '#hn .post a'
hacker_news_links.should_not be_empty
hacker_news_links.each do |link|
link[:href].should match(%r{http://news\.ycombinator\.com})
end
end
end
$ rspec spec/requests/homepage_spec.rb
F
Failures:
1) The homepage displays recent posts from Hacker News
Failure/Error: visit '/'
ActionController::RoutingError:
No route matches [GET] "/"
# ./spec/requests/homepage_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.05392 seconds
1 example, 1 failure
Sample::Application.routes.draw do
root to: 'posts#index'
end
#hn
- @recent_posts.each do |post|
.post
= link_to post.title, post.url
class PostsController < ApplicationController
def index
@recent_posts = Post.recent
end
end
$ rspec spec/requests/homepage_spec.rb F
Failures:
1) The homepage displays recent posts from Hacker News
Failure/Error: visit '/'
NameError:
uninitialized constant PostsController::Post
# ./app/controllers/posts_controller.rb:3:in `index'
# ./spec/requests/homepage_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.07371 seconds
1 example, 1 failure
require 'spec_helper'
describe Post do
describe '.recent' do
before do
posts = [
{ title: 'Post #1', url: 'http://example.com/1' },
{ title: 'Post #2', url: 'http://example.com/2' },
{ title: 'Post #3', url: 'http://example.com/3' }
]
HN::Post
.should_receive(:recent)
.and_return(posts)
@recent_posts = Post.recent
end
it 'returns recent posts' do
@recent_posts.should have(3).posts
end
it 'sets their title' do
@recent_posts.each do |post|
post.title.should be
end
end
it 'sets their url' do
@recent_posts.each do |post|
post.url.should be
end
end
end
end
module HN
class Post
end
end
class Post
def self.recent
posts = HN::Post.recent
posts.collect do |post|
new post
end
end
attr_accessor :title,
:url
def initialize(attributes)
self.title = attributes[:title]
self.url = attributes[:url]
end
end
In order to find interesting links for the intellectually curious
As a user
I want to see the latest Hacker News posts on the homepage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment