rspec controller测试

1 对于controller render/redirect的测试,一般对应以下的测试方法

render :action => :index
response.should render_template('index')

render :partial => 'post'
response.should render_template('_post')

redirect_to login_path
response.should redirect_to(login_path)
但是对于render :nothing = true来说,并没有相应的方法来测试,也无法用render_template来解决,只能是判断返回的response的内容是不是为空了。

response.should have_text(' ')
注意是 ,不是,至于为什么有个空格?我也没有仔细研究

response.body.should =~ /Listing widgets/m
response.body.should eq("")

2 对于controller with invalid params的测试,一般对应以下的测试方法
Member.any_instance.stub(:delete).and_return(false)
3 控制器中方法的调用
subject.send(:log_in,@user)


----------------------------------------实例-----------------------------
1 控制器中的 before_filter
class MyController < ApplicationController
before_filter :logged_in?

def index
end
end


describe MyController do
describe "GET 'index'" do
context "when not logged in"
# you want to be sure that before_filter is executed
it "requires authentication" do
controller.expects :logged_in?
get 'index'
end
# you don't want to spec that it will redirect you to login_path
# because that spec belongs to #logged_in? method specs
end
context "when authenticated" do
before(:each) { controller.stubs :logged_in? }
it "renders :index template" do
get 'index'
should render_template(:index)
end
it "spec other things your action does when user is logged in"
end
end
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值