How to stub Rspec

      @page = double(:page)
      %w(most_popular_content lead primary_tab_data recent_companies_data show_coroflot? coroflot).map do |method|
        @page.stub!( method.to_sym )
      end
      @page.stub(:channel_name).and_return('technology')
      view.stub(:admin?)
      @channel_name = 'technology' 
      (Dir["#{Rails.root}/app/views/shared/*.haml"] + Dir["#{Rails.root}/app/views/channels/*.haml"]).each do |file|
        stub_template file.gsub("#{Rails.root}/app/views/", '') => "" unless file =~ /channels\/show/
      end




      Article.should_receive(:head_versions).exactly(3).times.and_return(stub.as_null_object)




    describe "for users who are admins" do
      before(:each) do
        admin = FactoryGirl.create(:user, :email => "[email protected]", :admin => true)
        test_sign_in(admin)
      end

    it "should show edit link" do
      get 'index'
      Hunt.each do |hunt|
        response.should have_selector('a', :href => "edit",
                                       :content => "edit")    
      end
    end

    it "should show delete link" do
      get 'index'
      Hunt.each do |hunt|
        response.should have_selector('a', :href => "delete"  ,
                                       :content => "delete")  
      end
    end

RSpec::Matchers::define :have_title do |text|
  match do |page|
    Capybara.string(page.body).has_selector?('title', text: text)
  end
end

subject { page }
it { should have_title("My Title") }
it { should_not have_title("My Title") }



require 'spec_helper'
 
describe "posts/new.html.erb" do
  before(:each) do
    @post = assign(:post, stub_model(Post)).as_new_record.as_null_object
  end
  it "renders the form partial" do
    render
    rendered.should have_selector('form',:method => "post",:action => posts_path) do |form|
      form.should have_selector('label',:for=>'post_title',:content=>'Title')
      form.should have_selector('input',:type => "text",:name=>'post[title]',:id=>'post_title')
      form.should have_selector('label',:for=>'post_description',:content=>'Description')
      form.should have_selector('textarea',:cols=>'40',:rows=>'20',:name=>'post[description]',:id=>'post_description')
      form.should have_selector('input',:type=>'submit',:value=>'Publish')
    end
  end
 
end


require 'spec_helper'
 
describe "posts/show.html.erb" do
  before(:each) do
    @post = assign(:post, stub_model(Post,:id=>1,:title => "Test",:description=>'Want to contribute Engines'))
  end
  it "displays the post title with description" do
    render
    rendered.should contain("Test")
    rendered.should contain("Want to contribute Engines")
  end
  
  it "displays the  edit link" do
    render
    rendered.should have_selector('a',:href=>edit_post_path(@post.id),:content => 'Edit')
  end
  
  it "displays the back link to list the post" do
    render
    rendered.should have_selector('a',:content=>'Back',:href=>posts_path)
  end
  
end

response.should have_tag("li:nth-child(2)", :text => "I'm element 2")
response.should have_tag("li:nth-last-child(2)", :text => "I'm element 4")
response.should have_tag("ul:first-child", :text => "I'm element 1")
response.should have_tag("ul:last-child", :text => "I'm element 5")


require "spec_helper"

describe "gadgets/list" do
  it "renders the gadget partial for each gadget" do
    assign(:gadgets, [
      mock_model(Gadget, :id => 1, :name => "First"),
      mock_model(Gadget, :id => 2, :name => "Second")
    ])
    stub_template "gadgets/_gadget.html.erb" => "<%= gadget.name %><br/>"
    render
    expect(rendered).to match /First/
    expect(rendered).to match /Second/
  end
end



https://www.relishapp.com/rspec/rspec-rails/docs/view-specs/stub-template



class SessionsController < ApplicationController 
  def create 
    @user = User.find_by_auth_hash(auth_hash) 
  end 

  def auth_hash 
    request.env['omniauth.auth'] 
  end 
end 

describe SessionsController do 
  it 'should allow login' do 
    controller.stub!(:auth_hash).and_return({'provider' => 'twitter', 'uid' => '1234'}) 
    get :create, :provider => 'twitter' 
    assigns(:user).should_not be_nil 
  end 
end 


describe StickiesController do
  describe "GET index" do
    it "should assign stickies" do
      get :index
      assigns(:stickies).should_not be_nil
    end
  end
end

it "should work" do
   helper.stub(:sort_direction)
   helper.sort_link(...).should == ...
end


view
helper
stub

你可能感兴趣的:(rspec)