所以除了好奇之外,我没有什么充分的理由需要知道这一点——最好的理由——但我不确定这里发生了什么。
背景:
我正在研究 RSpec 书并更新示例。
第 24 章 - Rails 控制器有一个消息控制器的测试。
## 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
这与消息控制器一起使用:
## 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
当我运行测试时:
测试通过response.
it "renders the new template" do
response.should render_template("new")
end
测试也通过了subject.
it "renders the new template" do
subject.should render_template("new")
end
测试也通过了page
it "renders the new template" do
page.should render_template("new")
end
测试也通过了NOTHING
it "renders the new template" do
should render_template("new")
end
如果它可以帮助任何人弄清楚这一点,config/routes.rb
刚刚有 resources :messages
为什么所有这些测试都能通过?我实际上在测试什么? “页面”、“主题”和“ ”只是响应的同义词吗?
只要我的测试通过就重要吗?