Rspec/Capybara:测试是否调用了控制器方法 [英] Rspec / Capybara: Testing if a controller method is called
问题描述
假设我设置了一个带有索引操作的 HomeController
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
带有以下消息
Failure/Error: HomeController.should_receive(:index)
(<HomeController (class)>).index(any args)
expected: 1 time
received: 0 times
?是不是因为index方法是作为实例方法而不是类方法调用的?
? 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.
如果你想确保你的根路径被路由到 home#index
操作,路由规范可能是合适的:
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
请求
如果您想确保 index
模板在对根路径的请求中呈现,请求规范可能是合适的:
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
控制器
如果您想确保 index
模板在对 HomeController
的 index
操作的请求时呈现,控制器规范可能是合适的(在这种情况下与请求规范非常相似,但只关注控制器):
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
功能
如果您想确保由 home#index
呈现的页面具有某些特定内容,特性规范可能是合适的(也是唯一可以使用 Capybara 方法,如visit
,取决于你的 Rails/RSpec 版本):
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屋!