当我登录我的应用程序时,服务器向我发回cookie(凭据和一些应用程序的cookie):
- Response sent 170 bytes of Cookie data:
- Set-Cookie: user_credentials=val; path=/; HttpOnly; Secure
- Response sent 554 bytes of Cookie data:
- Set-Cookie: _app_session=otherVal; path=/; HttpOnly; Secure
…然后重定向到主页;
Cookie包括一些标志:例如httpOnly,安全等
如何测试cookie是否包含Rspec的那些标志?
至少我在哪里可以找到这些饼干?
- it "should generate cookies with proper flags" do
- params = Factory.attributes_for(:user,:username => "uname",:password => "upass"
- )
- # login
- post 'create',params
- response.should redirect_to home_url # => pass
- puts "response cookie = #{response.cookies.inspect}" # => {} // no cookies in response,why?
- end
解决方法
控制器规范不生成/调用真实的http请求,它们只是设置被测控制器并在其上调用所请求的操作.没有发出http请求,也没有生成真正的http答案.因此,您只能在更抽象的级别上测试Rails控制器的内部工作方式.
这些规范中的cookie处理相当简单,在这样的动作中设置cookie:
- def set_cookies
- cookies[:foo] = 'bar'
- cookies[:lorem] = {:value => 'ipsum',:expires => 3.days.from_now}
- render :nothing => true
- end
导致规范中可访问以下值:
- it "should set some cookie values" do
- get :set_cookies
- # response.cookies looks like this:
- # {'foo' => 'bar','lorem' => 'ipsum'}
- response.cookies['foo'].should == 'bar'
- response.cookies['lorem'].should == 'ipsum'
- end