简体   繁体   English

伪造RSpec和Capybara特征规范中的实例变量

[英]Faking instance variable in RSpec and Capybara feature spec

I'm trying to set up some feature specs before I get into refactoring some of my company's old code. 在我重构一些公司的旧代码之前,我正在尝试设置一些功能规范。 It's kind of an unconventional setup, but I was able to figure out enough about test doubles to bypass the authentication enough to get started. 这是一种非传统的设置,但我能够弄清楚测试双打足以绕过身份验证以便开始。 One problem I'm still having is that some of the instance variables set in these methods I'm bypassing are expected by the view, so I get undefined method for nil:NilClass errors. 我仍然遇到的一个问题是,我正在绕过的这些方法中设置的一些实例变量是视图所期望的,因此我得到undefined method for nil:NilClass错误。 I would like to get the specs running before I make any changes to the program code. 我想在对程序代码进行任何更改之前运行规范。 In this case, I could easily just move the particular instance variable to another method. 在这种情况下,我可以轻松地将特定的实例变量移动到另一个方法。 But I'm sure more situations like this will come up. 但我相信会有更多像这样的情况出现。 Here's the example I'm currently working on: 这是我目前正在研究的例子:

def security_level
  @right_now = Time.now

  #
  # other code that wont work without
  # connecting to a remote authentication
  # server
  #

end

Then in my spec: 然后在我的规范中:

feature 'Navigation' do
  before(:each) do
    allow_any_instance_of(ApplicationController).to receive(:security_level).and_return(nil)
  end
  scenario 'is possible' do
    visit root_path
    expect(page.has_content?('Quick Stats'))
  end
end

Here's the error, coming from @right_now.year in the view 这是来自视图中@right_now.year的错误

 Failure/Error: visit root_path
 NoMethodError:
   undefined method `year' for nil:NilClass
 # ./common/views/layouts/bootstrap/layout.haml:63

EDIT: Is there a way to set instance variables on the controller from within a feature spec? 编辑:有没有办法在功能规范中设置控制器上的实例变量?

There's no easy way to accomplish what you want. 没有简单的方法来实现你想要的。

The feature spec is handled mostly by Capybara, not RSpec. feature规范主要由Capybara处理,而不是RSpec。 Capybara runs the majority of the browser / rails server behavior in an external process. Capybara在外部进程中运行大部分浏览器/ rails服务器行为。 This make it inaccessible from RSpec's point-of-view. 这使得从RSpec的观点来看它无法访问。 Thus you cannot use stubs / doubles in this manner. 因此,您不能以这种方式使用存根/双打。

Feature specs are largely meant to be end-to-end acceptance tests. 功能规格主要是端到端验收测试。 The idea is to exercise your system as those who would use your system do. 我们的想法是将您的系统视为使用您的系统的人。 Generally, in these types of specs you perform various "workflows". 通常,在这些类型的规范中,您可以执行各种“工作流程”。 This means, having the spec, log a user in, navigate to particular pages, filling forms, clicking buttons and links. 这意味着,拥有规范,记录用户,导航到特定页面,填写表单,单击按钮和链接。 You then generally make your expectations on what you see in the view. 然后,您通常会对视图中显示的内容产生期望。

This means your spec would look more like: 这意味着您的规格看起来更像:

feature 'Navigation' do
  let(:regular_user) { User.create!(name: 'A Regular User') }

  def sign_in(a_user)
    visit sign_in_url
    # fill out form
    click_button 'Sign In'
  end

  before(:each) do
    sign_in(regular_user)
  end

  scenario 'is possible' do
    visit root_path
    expect(page.has_content?('Quick Stats'))
  end
end

https://github.com/per-garden/fakeldap可以为您的功能测试提供足够的ldap功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM