rspec,请求规范,设计,多控制器

时间:2014-07-30 21:30:21

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

我正在尝试构建一个请求规范,该规范测试通过设计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中的变量::基本控制器。在spec / request文件夹中运行specs时,我目前只有nil作为请求,我需要运行规范。

编辑:

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

1 个答案:

答案 0 :(得分:1)

我在发布我的问题之后不久就发现了这一点。感谢几个SO参考〜>请求spec exlish:http://goo.gl/iBg7v1&&在请求规范中设置http basic auth的请求标头:http://goo.gl/hdDBMd

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

规格/请求/ 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