ted*_*ted 8 ruby rspec ruby-on-rails
当我登录我的应用程序时,服务器向我发回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
Run Code Online (Sandbox Code Playgroud)
...然后重定向到主页;
饼干包括一些标志:如httpOnly,Secure等.
如何测试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
Run Code Online (Sandbox Code Playgroud)
控制器规范不生成/调用真正的 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
Run Code Online (Sandbox Code Playgroud)
导致规范中可访问以下值:
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
Run Code Online (Sandbox Code Playgroud)
要测试您在响应中看到的cookie标记的类型,您必须使用能够执行真实 http请求的内容.也许你可以使用水豚宝石吗?