Skip to content

Instantly share code, notes, and snippets.

View xirukitepe's full-sized avatar
🙈
See No Evil

Shi xirukitepe

🙈
See No Evil
View GitHub Profile
@xirukitepe
xirukitepe / request_rspec.rb
Created November 7, 2012 06:54
example usage of assert_select and
describe "home page" do
it "displays the user's username after successful login" do
user = User.create!(:username => "jdoe", :password => "secret")
get "/login"
assert_select "form.login" do
assert_select "input[name=?]", "username"
assert_select "input[name=?]", "password"
assert_select "input[type=?]", "submit"
end
@xirukitepe
xirukitepe / gist:4029912
Created November 7, 2012 06:57
Example test with capybara and factory girl
describe "home page" do
it "displays the user's username after successful login" do
user = FactoryGirl.create(:user, :username => "jdoe", :password => "secret")
visit "/login"
fill_in "Username", :with => "jdoe"
fill_in "Password", :with => "secret"
click_button "Log in"
page.should have_selector(".header .username", :text => "jdoe")
end
@xirukitepe
xirukitepe / controller_spec.rb
Created November 7, 2012 07:04
example on how to rspec test a controler
#with fixtures
describe WidgetsController do
describe "GET index" do
fixtures :widgets
it "assigns all widgets to @widgets" do
get :index
assigns(:widgets).should eq(Widget.all)
end
end
@xirukitepe
xirukitepe / fixture_example.rb
Created November 7, 2012 07:12
Example usage of Fixture
#To see the fixtures applied in a practical example, suppose we want to test a chat's application that displays some messages. For this #functionality we write the following spec:
#spec/views/chats/show.html.erb_spec.rb
describe "views/chats/show.html.erb" do
fixture :chats, :messages
before(:each) do
@chat = chats(:myChat)
render "/chats/show.html.erb"
end
@xirukitepe
xirukitepe / view_spec.rb
Created November 7, 2012 07:45
Example rspec test for a view
describe "events/index" do
it "renders _event partial for each event" do
assign(:events, [stub_model(Event), stub_model(Event)])
render
view.should render_template(:partial => "_event", :count => 2)
end
end
describe "events/show" do
it "displays the event location" do
@xirukitepe
xirukitepe / model_spec.rb
Created November 7, 2012 08:08
Sample rspec test for model
describe Article do
describe ".recent" do
it "includes articles published less than one week ago" do
article = Article.create!(:published_at => Date.today - 1.week + 1.second)
Article.recent.should eq([article])
end
it "excludes articles published at midnight one week ago" do
article = Article.create!(:published_at => Date.today - 1.week)
Article.recent.should be_empty
@xirukitepe
xirukitepe / route_spec.rb
Created November 7, 2012 08:09
Sample rspec test for routes
describe "routing to profiles" do
it "routes /profile/:username to profile#show for username" do
{ :get => "/profiles/jsmith" }.should route_to(
:controller => "profiles",
:action => "show",
:username => "jsmith"
)
end
it "does not expose a list of profiles" do
@xirukitepe
xirukitepe / helper_spec.rb
Created November 7, 2012 08:10
Helper spec example
describe EventsHelper do
describe "#link_to_event" do
it "displays the title, and formatted date" do
event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27))
# helper is an instance of ActionView::Base configured with the
# EventsHelper and all of Rails' built-in helpers
helper.link_to_event.should =~ /Ruby Kaigi, 27 Aug, 2010/
end
end
end
@xirukitepe
xirukitepe / mocks_and_stubs.rb
Created November 7, 2012 08:25
mocks and stubs example in rspec
#Mocks and stubs way
#The other option is to use mocks and stubs. Although there is a lot of information in the web about it, to help our discussion, I will #describe it very shortly. From my experience & research, I understand that the main difference between those two is the following:
#Stubbing a method is all about replacing the method with code that returns a specified result (or perhaps raises a specified exception). #Mocking a method is all about asserting that a method has been called (perhaps with particular parameters).
#As you can see, stubbing lets you define methods that are not currently implemented or we decided not to depend on. On the other hand, as we #mock objects we specify the behavior that we would expect on the mocked object, making our test more Behavior Driven Development.
#This is the recommended way of using Rspec as it defines the way the objects must collaborate to accomplish a certain task.
@xirukitepe
xirukitepe / reviewer_1_rspec.rb
Created November 7, 2012 08:50
my personal reviewer about rspec basics!
#RSpec gives you a way to encapsulate what you’re testing via the describe block, and it’s friend context.
#In a general unit testing sense, we use describe to describe the behavior of a class:
describe Hash do
end
#Tests are written using the it block. Here’s an example of how you might write a spec for the Hash class: