当我继续学习使用RSpec 2和Rails 3.1的TDD时,我似乎无法找到解决此问题的方法。带RSpec和Rails的TDD:测试控制器没有视图的动作
我有一个用户控制器与新的和创建操作。在我的UsersController天赋,我有
users_controller_spec.rb
describe "POST 'create'" do
before(:each) do
@attr = Factory.attributes_for(:user)
end
it "should assign an @user variable" do
post :create, :user => @attr
assigns[:user].should_not be_nil
assigns[:user].should be_kind_of(User)
end
end
,并在我的UsersController,
users_controller.rb
def create
@user = User.new(params[:user])
end
此规范与
失败1) UsersController POST 'create' should assign an @user variable
Failure/Error: post :create, :user => @attr
ActionView::MissingTemplate:
我可以继续实现应用程序代码以通过此测试,但我觉得此测试应该按原样传递。
有什么建议吗?
我见,我认为测试会(并且应该)通过,因为它确实分配了一个@user变量。换句话说,应用程序代码正在做我正在测试的内容。看起来我需要实现比测试需要传递更多的应用程序代码,因为我还没有测试渲染模板或用户重定向到的位置。 – Feech
没错。在测试中有错误和失败。你的测试没有遇到失败(如果@user是'nil'或者不是'User'),那么测试就会失败。失败或错误被认为是不成功的测试。 – aNoble
感谢您的帮助aNoble。我想我会保持我的控制器测试模式,首先测试正确的渲染/重定向,然后继续发生其他事情。 – Feech