如何确定rspec控制器测试的主题? [英] How can I determine the subject of an rspec controller test?

查看:68
本文介绍了如何确定rspec控制器测试的主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,除了好奇心外,我没有其他需要知道的很好的理由-最好的理由-但我不确定这里发生了什么。

So I don't have a great reason for needing to know this other than curiosity - the BEST reason - but I'm not sure what's going on here.

背景:

我正在研究RSpec书并更新示例。

Background:
I'm working through the RSpec book and updating the examples.

在第24章-Rails Controllers中,有一个对消息控制器的测试。

On Chapter 24 - Rails Controllers there's a test for a messages controller.

## spec/controllers/messages_controller_spec.rb ##

require 'spec_helper'

describe MessagesController do
  describe "POST create" do
    let(:message) { mock_model(Message).as_null_object }

    before do
      Message.stub(:new).and_return(message)
    end

  # Then a bunch of Tests...

    context "when the message fails to save" do
      before do
        message.stub(:save).and_return(false)
        post :create
      end

      it "assigns @message" do
        assigns[:message].should eq(message)
      end

      it "renders the new template" do
        response.should render_template("new")
      end
    end
  end
end

这与消息控制器一同出现:

This goes along with the messages controller:

## app/controllers/messages_controller.rb ##

class MessagesController < ApplicationController
  def create
    @message = Message.new(params[:message])

    if @message.save
      flash[:notice] = "The message was saved successfully"
      redirect_to action: "index"
    else
      render "new"
    end
  end
end



运行测试时:


测试通过响应

it "renders the new template" do
  response.should render_template("new")
end

测试也通过主题通过。

it "renders the new template" do
  subject.should render_template("new")
end

测试还通过页面

it "renders the new template" do
  page.should render_template("new")
end

测试还通过了没有

The test ALSO passes with NOTHING

it "renders the new template" do
 should render_template("new")
end

如果它可以帮助任何人搞砸, config / routes.rb 只需资源:messages



为什么所有这些测试都能通过?我实际上在测试什么? page, subject和’只是同义词吗?
只要考试通过,这有关系吗?

In case it helps anyone make heads or tails of this, the config/routes.rb just has resources :messages

Why do all those tests pass? What am I actually testing? Are 'page', 'subject', and ' ' just synonyms for response? Does it matter as long as my tests pass?

推荐答案

默认情况下,主题将引用该类,即MessagesController。

By default, the subject would be referencing the class, which is the MessagesController.

在最后一个测试示例中未定义主题,则将主题隐式设置为MessagesController。

Not defining a subject in the last test example, will implicitly set the subject to be MessagesController.

这篇关于如何确定rspec控制器测试的主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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