rspec Rails 模拟会话哈希

发布于 2024-09-18 23:08:38 字数 769 浏览 4 评论 0原文

我试图模拟控制器的会话哈希,如下所示:

it "finds using the session[:company_id]" do
  session.should_receive(:[]).with(:company_id).and_return 100
  Company.should_receive(:find).with(100)
  get 'show'
end

当我调用 get 'show' 时,它指出:

received :[] with unexpected arguments  
expected: (:company_id)  
   got: ("flash")

控制器代码看起来像:

def show
  company_id = session[:company_id]
  @company = Company.find params[company_id]
end

我也简单地尝试过设置

it "finds using the session[:company_id]" do
  session[:company_id]= 100
  Company.should_receive(:find).with(100)
  get 'show'
end

,但随后遇到了一个问题:

expected: (100)
got: (nil)

有人知道为什么吗?

I am trying to mock out the session hash for a controller like so:

it "finds using the session[:company_id]" do
  session.should_receive(:[]).with(:company_id).and_return 100
  Company.should_receive(:find).with(100)
  get 'show'
end

When I call get 'show' it states:

received :[] with unexpected arguments  
expected: (:company_id)  
   got: ("flash")

The controller code looks like:

def show
  company_id = session[:company_id]
  @company = Company.find params[company_id]
end

I have also simply tried setting

it "finds using the session[:company_id]" do
  session[:company_id]= 100
  Company.should_receive(:find).with(100)
  get 'show'
end

but then get an issue about:

expected: (100)
got: (nil)

Anyone have ideas why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌上青苔 2024-09-25 23:08:38

我刚刚遇到了这个。我无法设法让 should_receive 不干扰闪存的东西。

但这让我测试了我正在寻找的行为:

it "should redirect to intended_url if set" do
  request.env['warden'] = double(:authenticate! => true)
  session.stub(:[]).with("flash").and_return double(:sweep => true, :update => true, :[]= => [])
  session.stub(:[]).with(:intended_url).and_return("/users")
  post 'create'
  response.should redirect_to("/users")
end

希望有帮助......

I just ran into this. I couldn't manage to get should_receive to not interfere with the flash stuff.

But this let me test the behavior I was looking for:

it "should redirect to intended_url if set" do
  request.env['warden'] = double(:authenticate! => true)
  session.stub(:[]).with("flash").and_return double(:sweep => true, :update => true, :[]= => [])
  session.stub(:[]).with(:intended_url).and_return("/users")
  post 'create'
  response.should redirect_to("/users")
end

Hope that helps...

窝囊感情。 2024-09-25 23:08:38

我不知道如何模拟会话容器本身,但是在大多数情况下,只需通过请求传递会话数据就足够了。因此测试将分为两种情况:

it "returns 404 if company_id is not in session" do
  get :show, {}, {}
  response.status.should == 404 # or assert_raises depending on how you handle 404s
end

it "finds using the session[:company_id]" do
  Company.should_receive(:find).with(100)
  get :show, {}, {:company_id => 100}
end

PS:忘记提及我正在使用此片段中的一些自定义帮助器。

I could not figure out how to mock the session container itself, however in most cases simply passing session data with request should be enough. So the test would split into two cases:

it "returns 404 if company_id is not in session" do
  get :show, {}, {}
  response.status.should == 404 # or assert_raises depending on how you handle 404s
end

it "finds using the session[:company_id]" do
  Company.should_receive(:find).with(100)
  get :show, {}, {:company_id => 100}
end

PS: forgot to mention I'm using some customized helpers from this snippet.

三生池水覆流年 2024-09-25 23:08:38

试试这个:

session.expects(:[]).with(has_entries('company_id' => 100))

try this:

session.expects(:[]).with(has_entries('company_id' => 100))
伪心 2024-09-25 23:08:38

这是因为您从控制器获取闪存会话。所以定义它。 Flash 保存在会话中。

it "finds using the session[:company_id]" do
  session.stub!(:[]).with(:flash)
  session.should_receive(:[]).with(:company_id).and_return 100
  Company.should_receive(:find).with(100)
  get 'show'
end

It's because you fetch flash session from your controller. So define it. Flash is save in session.

it "finds using the session[:company_id]" do
  session.stub!(:[]).with(:flash)
  session.should_receive(:[]).with(:company_id).and_return 100
  Company.should_receive(:find).with(100)
  get 'show'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文