【问题标题】:rspec, request spec, devise, multi-controllerrspec,请求规范,设计,多控制器
【发布时间】:2014-07-30 21:30:20
【问题描述】:

我正在尝试构建一个请求规范,以测试通过设计 API 创建新用户的全部范围。

我目前在 RegistrationsController 规范中有这个,但如果我想按照邮件链接到确认控制器,这将不起作用。

我无法找到一个很好的例子来说明人们如何测试从一个控制器到另一个控制器的“切换”以及间歇性“步骤”(我们在整个过程中散布了自定义设计方法,该测试将也包括在内)。

it "creates a user, sends a welcome email, confirms the user, and registers the user in email campaigns" do
  post :create, {user: new_user_params}
  last_email = ActionMailer::Base.deliveries.last.body

  ConfirmationsController.any_instance.should_receive(:after_filter_method_to_subscribe_user)

  redirect_to confirmation_link(last_email) # helper method

  last_email.should include("Thanks for joining!")
  user = User.find_by_first_name(new_first_name)
  user.confirmed?.should be_true
  user.email_lists.should_not be_empty
end

编辑:

我还应该补充一点,我需要 http_basic_auth 来运行我包含在规范/支持文件中的规范,并将 request.env['HTTP_AUTHORIZATION'] 设置为存储在 API::Base 控制器中的变量。我目前在 spec/request 文件夹中运行规范时将 nil 作为请求对象,我需要运行规范。

编辑:

感谢看过的人。在将两个 SO 搜索和我拥有的代码拼凑在一起后,我想通了。我会尽可能为未来的 SO'ers 发布答案。

【问题讨论】:

  • 我认为您更应该关注功能/集成规范。
  • @MichalSzyndel,是的,我在第一行提到了将其作为请求规范(这是集成测试的薄包装器)-抱歉-我意识到我的示例使它有点混乱。实际上,我通过拼凑我的答案的两个资源来解决这个问题。请求规范:link && basic_auth 请求规范:link。感谢回复!

标签: ruby-on-rails rspec devise integration-testing


【解决方案1】:

我在发布我的问题后不久就发现了这一点,并在更多谷歌搜索中发现了好运。感谢几个 SO 参考 ~> 请求规范津津乐道:http://goo.gl/iBg7v1 && 在请求规范中为 http 基本身份验证设置请求标头:http://goo.gl/hdDBMd

我的规范看起来像下面这样希望这可以帮助别人不要像我一样浪费 4 个小时:)。

spec/requests/api/user_registration_spec.rb。

it "sends a welcome email, confirms the user, and signs the user up to email campaigns" do
  email_list = FactoryGirl.create(:email_list, name: "funky-email-campaign")
  user_name  = Api::RegistrationsController::USER
  password   = Api::RegistrationsController::PASSWORD

  # post to /users/registration
  post api_registrations_path({user: new_user_params}), nil , {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user_name, password)}

  last_email = ActionMailer::Base.deliveries.last.body

  UserService.should_receive(:subscribe_to_email).and_call_original # check that after_filter is called

  get confirmation_link(last_email) # follow link in email (/users/confirmation)

  response.should redirect_to(custom_path) # tests after_confirmation_path_for override
  last_email.should include(new_first_name)
  last_email.should include("Thanks for joining!")
  user = User.find_by_first_name(new_first_name)
  user.confirmed?.should be_true
  user.email_lists.first.name.should eq(email_list.name)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    相关资源
    最近更新 更多