RSpec控制器测试的模拟模型和Stubbing

时间:2013-08-03 19:24:57

标签: ruby-on-rails rspec

我正在使用RSpec在我的Viewings模型的控制器上测试create方法。我正在尝试创建一个模拟查看模型,以最小化对我的数据库的调用,删除其新的和保存方法,并返回适当的值来测试我的控制器的逻辑是否正常工作:

describe 'POST #create' do
    let(:office_listing) { create(:office_listing) }
    let(:viewing) { mock_model(Viewing).as_null_object }
····
    before do·
      Viewing.stub(:new).and_return(viewing)
    end

    describe 'with valid attributes' do
      it 'saves the viewing' do·
        Viewing.any_instance.stub(:save).and_return(true)
        viewing.should_receive(:save)
        post :create, office_listing_id: office_listing.id
      end
    end

    describe 'with invalid attributes' do
      it 'fails and renders a failure message' do
        Viewing.any_instance.stub(:save).and_return(false)
        viewing.should_receive(:save)
        post :create, :office_listing_id => office_listing.id
        assigns(:failure_message).should_not be_nil
      end
    end
  end
end

这是我的控制器代码:

def create
    @viewing = Viewing.new(params[:viewing])
    @viewing.broker = current_broker
    @viewing.office_listing = params[:office_listing_id]

    p @viewing.save

    if @viewing && @viewing.save == true
      respond_to do |format|
        format.js
        format.html { redirect_to root_path }
      end
    else
      respond_to do |format|
        @failure_message = "Unable to create viewing."
        format.js
        format.html { redirect_to root_path }
      end
    end
  end
end

问题在于,即使我已经根据我所在的测试取消了存储以返回true或false,它总是返回一个查看对象,这使我相信存根不起作用并且控制器将我的模拟对象视为真实对象,在调用.save时进行数据库调用。我是否需要使用查看以外的类来删除方法?我应该只是调用viewing.stub而不是Viewing.any_instance?实际上我认为打字可能已经回答了我自己的问题,但我仍然希望听到任何人对这个问题的看法。

1 个答案:

答案 0 :(得分:3)

你不能同时使用

Viewing.any_instance.stub(:save)

viewing.should_receive(:save)

第一个用于控制流程,而第二个用于测试某些东西。

你应该这样做:

let!(:viewing) { Viewing.new } # no lazy load, executed right away, no need to mock

def action
  post :create, office_listing_id: office_listing.id
end

before do
  Viewing.stub(:new).and_return(viewing)
end

it "calls save" do
  expect(viewing).to receive(:save).and_return(true)
  action
end
相关问题