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 => "admin@example.com", :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



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




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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值