Ruby-on-rails 正在渲染的 Rspec 测试模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3810965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rspec testing templates being rendered
提问by Sid
Im trying to test a condition where on successful signup a Success Template is rendered by the following controller code
我正在尝试测试成功注册成功模板由以下控制器代码呈现的条件
def create
@user = User.new(params[:user])
if @user.save
render :template => "success"
else
flash[:notice] = "Oops Somethings not quite right! :("
render :action => "new"
end
end
I am using the following spec to test out this code
我正在使用以下规范来测试此代码
before(:each) do
@user = User.new
@user.attributes = valid_attributes
@params = valid_attributes
@user.stub!(:save).and_return(true)
end
def do_post
post :create
end
it "should create new user " do
count = User.count
do_post
user = User.new(@params)
user.save.should eql(true)
User.count.should eql(count + 1)
end
it "should render the success page on successful signup" do
do_post
@user.save
response.should render_template("success") if @user.save
end
But the example fails "it should render success page on successful signup" with this error message
但是示例失败“它应该在成功注册时呈现成功页面”并显示此错误消息
1)
'UsersController handling POST /users should render the success page on successful signup' FAILED
expected "success", got "users/new.html.erb"
./spec/controllers/users_controller_spec.rb:67:
The success view is an template stored in the views/users/ without an action. Im guessing im making a very fundamental mistake and would like some help .
成功视图是一个模板,存储在 views/users/ 中,没有任何动作。我猜我犯了一个非常根本的错误,需要一些帮助。
回答by Simone Carletti
You are stubbing the @uservariable in the test, but the controller will instantiate a new instance so the stub won't be in place.
您@user在测试中存根变量,但控制器将实例化一个新实例,因此存根不会就位。
It's not a good idea to use a stub in this case just to emulate a successful save call. Why don't you supply valid data instead and make sure the action is successful?
在这种情况下使用存根来模拟成功的保存调用并不是一个好主意。为什么不提供有效数据并确保操作成功?
The following code is for RSpec > 2.1and it uses the expectsyntax.
以下代码适用于RSpec > 2.1,它使用expect语法。
before(:each) do
@params = valid_attributes
end
it "should create new user" do
@_before = User.count
post :create, :user => @params
expect(assigns(:user)).to_not be_new_record
expect(User.count).to eq(@_before + 1)
end
it "should render the success page on successful signup" do
post :create, :user => @params
expect(response).to be_successful
expect(response).to render_template("success")
end
Finally, change
最后,改变
render :template => "success"
to
到
render :action => "success"
For previous RSpec versions or if you have to use the shouldsyntax, use
对于以前的 RSpec 版本,或者如果您必须使用should语法,请使用
before(:each) do
@params = valid_attributes
end
it "should create new user" do
@_before = User.count
post :create, :user => @params
assigns(:user).should_not be_new_record
User.count.should == (@_before + 1)
end
it "should render the success page on successful signup" do
post :create, :user => @params
response.should be_successful
response.should render_template("success")
end

