rspec

    use rspec filter to speed up tests

    21 Oct 2011

    Rspec 2 introduce a very efficient way to test only one test or one test suit, it's filter_run.

    You should first add filter_run in rspec/spec_helper.rb

    config.filter_run :focus => true

    Then you can tell rspec to test only one test you are focused by

    it "should focus now", :focus => true do
      ...
    end

    rspec will only test this spec, :focus => true can be applied on describe/context as well.

    One problem is that if there is no :focus => true on your tests, rspec will do nothing, but most of time we are...

    Read More

    Tags 


    get_response_with_headers

    30 Aug 2009

    最近在用ruby的net/http写爬虫,发现net/http提供的接口还真不是一般不好用

    一开始我是用Net::HTTP.get_response方法,挺简单的,测试起来也不难

    http = mock(Net::HTTPSuccess)
    http.stubs(:is_a?).with(Net::HTTPSuccess).returns(true)
    http.stubs(:body).returns(content)
    Net::HTTP.expects(:get_response).with(URI.parse(remote_path)).returns(http)

    接着碰到有些网站必须指定User-Agent才能访问,发现get_response方法不能修改http headers,只能换Net::HTTP.start方法

    response = start(uri.host, uri.port) do |http|
      http.get(uri.request_uri, headers)
    end

    可是写测试的时候傻眼了,因为要根据不同的uri.request_u...

    Read More

    Tags 


    Rspec测试render :nothing => true

    03 Jun 2009

    对于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(' ')

    注意是 ,不是,至于为什么有个空格?我也没有仔细研究

    Read More

    Tags