Ruby on rails RSpec控制器测试的模拟模型和存根

Ruby on rails RSpec控制器测试的模拟模型和存根,ruby-on-rails,rspec,Ruby On Rails,Rspec,我正在用RSpec在控制器上测试我的视图模型的create方法。我正在尝试创建一个模拟查看模型,以尽量减少对我的数据库的调用,删除其新方法和保存方法,并返回适当的值,以测试我的控制器的逻辑是否正常工作: describe 'POST #create' do let(:office_listing) { create(:office_listing) } let(:viewing) { mock_model(Viewing).as_null_object } ···· be

我正在用RSpec在控制器上测试我的视图模型的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
问题是,尽管我已根据我所处的测试将save去掉以返回true或false,但它始终返回一个查看对象,这使我相信stubing不起作用,控制器将我的模拟对象视为真实对象,在我调用.save时进行数据库调用。我是否需要使用Viewing以外的类来删除该方法?我应该调用viewing.stub而不是viewing.any_实例吗?事实上,我认为这可能回答了我自己的问题,但我还是想听听大家对这个问题的看法

不能两者都用

Viewing.any_instance.stub(: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
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