如何使用RSPEC在控制器方法中测试条件重定向

发布于 2025-01-30 16:11:20 字数 677 浏览 2 评论 0原文

我的控制器中有该方法,

  def promotional_sales
    redirect_to plans_path and return unless promotional_campaign_active?
    config_meta_tags({}, {
      title: "Subscription promotion in our website",
      description: "Do you want to know more about our website promotions? Stay on top of all promotions and guarantee your discount"
    })
    if control_flag[:phase] == "captation"
      setup_promotional_campaign
      render :promotion_3
    end
    if control_flag[:phase] == "sales"
      setup_courage_hotsite
      render :hotsite_1
    end
  end
end

如果将促销活动不活跃并且有效时,请编写一个RSPEC测试,如果重定向Plane_path将发生。

拜托,您能指导我如何编写此测试?

I have that method in my controller

  def promotional_sales
    redirect_to plans_path and return unless promotional_campaign_active?
    config_meta_tags({}, {
      title: "Subscription promotion in our website",
      description: "Do you want to know more about our website promotions? Stay on top of all promotions and guarantee your discount"
    })
    if control_flag[:phase] == "captation"
      setup_promotional_campaign
      render :promotion_3
    end
    if control_flag[:phase] == "sales"
      setup_courage_hotsite
      render :hotsite_1
    end
  end
end

I would like do write a RSpec test that cover if the redirection to plans_path is happening when promotional campaign is inactive and if is active goes to the next block.

Please, can you guide me how do I write this test?

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

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

发布评论

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

评论(1

痴情 2025-02-06 16:11:20

您可以使用 redirect_to Matcher。根据您的状况,您可以期望

在促销活动不活动时会重定向吗?

expect(response).to redirect_to(plans_path)

You can use the redirect_to matcher. Based on your condition you can expect response to be redirected or not

when promotional campaign is inactive

expect(response).to redirect_to(plans_path)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文