Rspec / Capybara:测试是否调用了控制器方法 [英] Rspec / Capybara: Testing if a controller method is called

查看:163
本文介绍了Rspec / Capybara:测试是否调用了控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我设置了一个具有索引操作的HomeController

  class HomeController< ApplicationController 
def index
@users = User.all
end
end


b $ b

并通过根路径路由到它。

  root:to => home#index

为什么此请求规范失败

  it应该调用home#index动作do 
HomeController.should_receive(:index)
访问root_path
end

 失败/错误:HomeController.should_receive(:index)
(< HomeController(class)>)。index(any args)
expected:1 time
received:0 times

?是因为索引方法被调用为一个实例方法而不是类方法?

解决方案

你想测试,我认为有什么方法可以在哪里使用的一些混乱,所以我会尝试和举例路由规范请求规范控制器规格功能规格

如果您想要确认您的根路径路由到 home#index 操作,路由规范可能是适当的:



spec / routing / routing_spec.rb

  describeRoutingdo 
itroutes / to home#indexdo
expect(get(/))to route_to(home#index)
end
end
/ pre>

请求



如果要确保 code>模板在请求到您的根路径时被请求,请求规范可能是适当的:



spec / requests / home_requests_spec.rb

 描述家庭请求do 
它成功地在GET /'do $ b上呈现索引模板$ b get/
expect(response).to be_successful
expect(response).to render_template(:index)
end
end



控制器



如果您要确保 > 的 index 控制器规范可能是合适的(并且在这种情况下非常类似于请求规范,但专注于控制器):



spec / controllers / home_controller_spec.rb

 描述HomeController do 
描述GET索引do
it索引模板do
expect(controller).to receive(:index)#this line maybe of dubious value
get:index
expect(response).to be_successful
expect响应)到render_template(:index)
end
end
end



功能



如果要确保 home#index 呈现的页面具有某些特定内容,功能规范可能是适当的(也是您可以使用 Capybara方法(例如访问,具体取决于您的Rails / RSpec版本):



spec / features / home_features_spec.rb >

 特性索引页do 
方案查看索引页do
访问root_path
expect (page).to have_text(Welcome to my awesome index page!)
end
end


Given I set up a HomeController with an index action

class HomeController < ApplicationController
  def index
    @users = User.all
  end
end

and routed to it via the root path,

  root :to => "home#index"

why does this request spec fail

it 'should called the home#index action' do
    HomeController.should_receive(:index)
    visit root_path
end

with the following message

 Failure/Error: HomeController.should_receive(:index)
   (<HomeController (class)>).index(any args)
       expected: 1 time
       received: 0 times

? Is it because the index method is called as a instance method instead of a class method?

解决方案

I'm not sure exactly what you want to test, and I think there's some confusion as to what methods can be used where, so I'll try and give examples of Routing specs, Request Specs, Controller specs, and Feature specs, and hopefully one of them will be appropriate for you.

Routing

If you want to make sure that your root path gets routed to the home#index action, a routing spec may be appropriate:

spec/routing/routing_spec.rb

describe "Routing" do
  it "routes / to home#index" do
    expect(get("/")).to route_to("home#index")
  end
end

Request

If you want to make sure that the index template gets rendered on a request to your root path, a request spec may be appropriate:

spec/requests/home_requests_spec.rb

describe "Home requests" do
  it 'successfully renders the index template on GET /' do
    get "/"
    expect(response).to be_successful
    expect(response).to render_template(:index)
  end
end

Controller

If you want to make sure that the index template gets rendered on a request to the index action of your HomeController, a controller spec may be appropriate (and quite similar to a request spec in this case, but focused exclusively on the controller):

spec/controllers/home_controller_spec.rb

describe HomeController do
  describe "GET index" do
    it "successfully renders the index template" do
      expect(controller).to receive(:index) # this line probably of dubious value
      get :index
      expect(response).to be_successful
      expect(response).to render_template(:index)
    end
  end
end

Feature

If you want to make sure the page rendered by home#index has some specific content, a feature spec may be appropriate (and also the only place you can use Capybara methods like visit, depending on your Rails/RSpec version):

spec/features/home_features_spec.rb

feature "Index page" do
  scenario "viewing the index page" do
    visit root_path
    expect(page).to have_text("Welcome to my awesome index page!")
  end
end

这篇关于Rspec / Capybara:测试是否调用了控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆