【发布时间】:2010-06-04 16:36:26
【问题描述】:
我在 Foundation Rails 2 书的第 10 章。我们正在与 RSpec 合作。
我们正在测试“PluginsController”的“索引”操作。
这是控制器的代码:
class PluginsController < ApplicationController
# GET /plugins
# GET /plugins.xml
def index
@plugins = Plugin.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @plugins }
end
end
这是该控制器的测试代码:
require File.dirname(__FILE__) + '/../spec_helper'
describe PluginsController, " GET to /plugins" do
before do
@plugin = mock_model(Plugin)
Plugin.stub!(:find).and_return([@plugin])
end
it "should be successful" do
get :index
response.should be_success
end
it "should find a list of all plugins" do
Plugin.should_receive(:find).with(:all).and_return([@plugin])
get :index
end
it "should assign the list of plugins to a variable to be used in the view"
it "should render the index template"
end
当我们编写测试时,我认为这一行
Plugin.should_receive(:find).with(:all).and_return([@plugin])
应该有
@plugins
而不是
@plugin
因为在控制器中我们有
def index
@plugins = Plugin.find(:all)
我想看看如果我改变了会发生什么
Plugin.should_receive(:find).with(:all).and_return([@plugin])
到
Plugin.should_receive(:find).with(:all).and_return([@plugins])
测试通过了。
那么...为什么是@plugin 而不是@plugins?还有...为什么两个都通过测试?
干杯!
【问题讨论】:
标签: ruby-on-rails rspec