Ruby on rails 3 Rspec2:response.should在无效参数失败后呈现_模板(“new”)

Ruby on rails 3 Rspec2:response.should在无效参数失败后呈现_模板(“new”),ruby-on-rails-3,rspec,rspec2,Ruby On Rails 3,Rspec,Rspec2,我正在RSpec2中测试控制器,对于我的创建和更新操作,当传递无效参数时,控制器应分别呈现“新建”或“编辑”模板。它正在这样做,但我的考试从来没有通过 describe "with invalid params" do before(:each) do User.stub(:new) { mock_user(:valid? => false, :save => false) } end it "re-renders the 'new' templ

我正在RSpec2中测试控制器,对于我的创建和更新操作,当传递无效参数时,控制器应分别呈现“新建”或“编辑”模板。它正在这样做,但我的考试从来没有通过

describe "with invalid params" do
    before(:each) do
      User.stub(:new) { mock_user(:valid? => false, :save => false) }
    end

    it "re-renders the 'new' template" do
      post :create, :company_id => mock_company.id
      response.should render_template("new")
    end
  end
结果如下:

re-renders the 'new' template
expecting <"new"> but rendering with <"">
此问题似乎也与此控制器隔离。我有几乎相同的代码运行另一个控制器,这是好的。我不确定问题出在哪里

更新: 下面是两个模拟方法

def mock_user(stubs={})
  @mock_user ||= mock_model(User, stubs).as_null_object
end

def mock_company(stubs={})
  (@mock_company ||= mock_model(Company).as_null_object).tap do |company|
    company.stub(stubs) unless stubs.empty?
  end
end

结果是存根和坎坎的问题。CanCan正在加载资源,并使用了一些与我想象中不同的方法。

你能发布
mock\u user
mock\u company
的内容吗?我们仍然缺少一些东西。如何在控制器中设置
@company
@user
的值(可能使用before过滤器?)。控制器是否真的调用了
User.new
?那是在三月份,所以我必须去挖掘它才能找到答案。而且我确信CanCan从那时起已经改变了很多,所以即使在那时,答案也可能与获得完全相同的错误无关,但不使用CanCan。至少我知道我不是孤独症,我仍然会犯这个错误,我使用的是CanCan和Rspec的最新版本。我刚从控制器上移除了CanCan过滤器,问题仍然存在
def mock_user(stubs={})
  @mock_user ||= mock_model(User, stubs).as_null_object
end

def mock_company(stubs={})
  (@mock_company ||= mock_model(Company).as_null_object).tap do |company|
    company.stub(stubs) unless stubs.empty?
  end
end