Rspec2: response.should render_template(“new”) after invalid params fails
我正在测试RSpec2中的一个控制器,对于我的创建和更新操作,当传递无效参数时,该控制器应分别呈现"新"或"编辑"模板。它正在执行此操作,但是我的测试从未通过。
1 2 3 4 5 6 7 8 9 10 | 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 |
结果如下:
1 2 | re-renders the 'new' template expecting <"new"> but rendering with <""> |
这是控制器动作:
1 2 3 4 5 6 7 8 9 10 11 12 13 | respond_to do |format| if @user.save format.html { flash[:notice] ="#{@user.full_name} was added to #{@company.name}." redirect_to company_users_url(@company) } else logger.debug @user.errors format.html{ render :new } end end |
此问题似乎也与此控制器无关。我有几乎相同的代码运行另一个控制器,这很好。我不确定问题可能在哪里。
更新:
这是两个模拟方法
1 2 3 4 5 6 7 8 9 | 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 |
原来这是Stubbing和CanCan的问题。 CanCan正在加载资源,并使用了一些与我想像不同的方法。