`

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



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 测试Rails 程序.pdf

    - **匹配器**:了解RSpec提供的各种匹配器,如`expect(object).to be_valid`等,以方便进行断言。 #### 四、使用预构件生成测试数据 - **对比预构件和固件**:预构件(Factories)是RSpec中一种生成测试数据的方法...

    cpp-stub 开源代码

    在这个特定的案例中,`cpp-stub` 存在一个在ARM平台上运行时的BUG,当尝试使用 `stub.h` 中的 `reset` 方法时,会触发段错误(Segmentation Fault),这是一个严重的问题,因为它会导致程序崩溃。 首先,我们来理解...

    cpp-stub 中文使用手册

    单元测试打桩开源库 cpp-stub 使用手册 中文版本,这是从git上直接下载的,git上下载的源代码在arm上调用Stub.reset方法会引发段错误,在资源cpp-stub开源代码(下载地址:...

    gtest stub 详细用法,附件用例,链接

    gtest stub 详细用法,附件用例,链接

    The RSpec book

    - 例如:`allow(MyClass).to receive(:method_name)` 或 `stub_const('CONST_NAME', value)` #### 七、RSpec 的社区支持 - **广泛的文档**:RSpec 官方文档非常详尽,覆盖了框架的所有方面。 - **活跃的社区**:...

    Android应用:StubView显示与隐藏

    在Android应用开发中,`StubView`是一种非常实用的组件,它允许我们在程序运行时动态地加载布局。在本文中,我们将深入探讨如何在Android应用中实现`StubView`的显示与隐藏,以及相关的源代码实践。 首先,`Stub...

    [PE变形]拓展STUB

    标题中的“[PE变形]拓展STUB”是指在PE(Portable Executable)文件格式中,对可执行文件的STUB部分进行变换的技术。PE文件格式是Windows操作系统中用于存放应用程序和动态链接库的主要格式。STUB,即Stub Section,...

    com.stub.StubApp.apk.1

    3. **Stub应用**:StubApp可能是为了测试或其他目的创建的占位符应用,不具备完整功能。 4. **版本控制**:".1.1"可能代表版本号,表明这是StubApp的一个更新版本。 5. **软件开发流程**:理解在开发过程中,开发者...

    com.stub.StubApp.apk

    com.stub.StubApp.apk

    grpc-stub-1.24.0-API文档-中文版.zip

    赠送jar包:grpc-stub-1.24.0.jar; 赠送原API文档:grpc-stub-1.24.0-javadoc.jar; 赠送源代码:grpc-stub-1.24.0-sources.jar; 赠送Maven依赖信息文件:grpc-stub-1.24.0.pom; 包含翻译后的API文档:grpc-stub-...

    使用axis1生成stub客户端样例

    本示例将详细介绍如何使用Axis1生成Stub客户端,以便与Web服务进行交互。 首先,让我们理解什么是Stub客户端。Stub是模拟服务器行为的本地代理,它允许客户端代码在没有实际连接到服务器的情况下进行测试或调用服务...

    1037571306982519com.stub.StubApp.apk

    1037571306982519com.stub.StubApp.apk

    IP路由-OSPF-Stub配置.docx

    OSPF 路由配置 - Stub 区域配置指南 OSPF(Open Shortest Path First)是一种链路状态路由协议,广泛应用于大型企业网络和ISP网络中。作为一种内部网关协议(IGP),OSPF 能够提供高效、可靠的路由选择和网络拓扑...

    通过stub例程实现远程过程调用

    在这个过程中,"stub"扮演了关键角色,它是本地和远程系统之间的中介。 Stub例程,也称为桩函数,主要有两个功能:本地接口适配和网络数据传输。当客户端发起RPC请求时,stub会处理参数的序列化,将它们转换为可以...

    RSpec-bank-main.rar

    在RSpec中,经常使用`double`、`stub`等工具来模拟对象的行为,以便专注于测试特定的功能。例如,在测试交易处理时,可能会模拟银行账户的`deposit`和`withdraw`方法,避免了实际的数据库操作。 七、集成测试与单元...

    com.stub.StubApp.apk.1.1

    com.stub.StubApp.apk.1.1

    Firefox Setup Stub 23.0.1

    Firefox Setup Stub 23.0.1

    rspec-activemodel-mocks:将stub_model和嘲笑模型添加到rspec-mocks

    RSpec :: ActiveModel ::... 要在没有rspec-rails的情况下使用stub_model和mock_model ,需要以下文件: require 'rspec/active_model/mocks'用法嘲笑创建一个测试双string_or_model_class ,表示string_or_model_class

    stub测试桩函数库 函数库

    Stub测试桩函数库是软件测试领域中一个重要的工具,它在单元测试中扮演着关键角色。测试桩(Stub)是用来模拟被测代码依赖的外部组件或服务的行为,以控制测试环境并提供预定义的响应。这样,我们可以在孤立的环境中...

Global site tag (gtag.js) - Google Analytics