English 中文(简体)
在Rspec中重新使用代码的最佳做法?
原标题:Best practice for reusing code in Rspec?

我使用 Rspec 和 Capybara 来写集成测试。 我注意到, 在测试主动记录选项的创建时, 我经常要执行相同的代码 。

例如:

it "should create a new instance" do
  # I create an instance here
end

it "should do something based on a new instance" do
  # I create an instance here
  # I click into the record and add a sub record, or something else
end

问题似乎是,主动记录对象在测试中并不存在,然而,Capybara默认会保持相同的分期(怪异)状态。

我可以模仿这些记录, 但是因为这是一个整合测试, 有些记录相当复杂(它们有图像附加物和什么的), 使用Capybara 填写用户信息表简单得多。

我试图定义一个功能 来创造新的记录, 但出于某种原因,这感觉并不正确。什么是最佳的做法?

问题回答

这里有几种不同的方式。首先,在两种情况下,你可以将示例块组合到描述或上下文块之下,例如:

describe "your instance" do
  it "..." do
    # do stuff here
  end

  it "..." do
    # do other stuff here
  end
end

然后,在描述或上下文块内,你可以设置可用于所有例子的状态,例如:

describe "your instance" do
  # run before each example block under the describe block
  before(:each) do
    # I create an instance here
  end

  it "creates a new instance" do
    # do stuff here
  end

  it "do something based on a new instance" do
    # do other stuff here
  end
end

作为前( each) 区块的替代, 您也可以使用 let helper, 我认为这更易读。 您可以看到更多有关它的信息 https:// www.relishap. com/ rspec/ rspec- core/ v/2- 10/docs/ helper- methods/let-and-let "在这里

满足你要求的最佳实践是使用 Factory Girl 从界定共同属性的蓝图和database_cleaner 中创建记录,通过不同的测试/标准来清理数据库。

使用 rspec 的 < code > - order < / code > 选项, 您可以发现这种依赖关系。 如果您的设置随机失灵, 您会有这样的问题 。

鉴于标题(.在Rspec中使用代码),我建议阅读RSpec 定制匹配者 在“铁路路边教学Ruby”中。

Michael Hartl提出两个解决方案,

  1. Define helper methods for common operations (e.g. log in a user)
  2. Define custom matchers

使用这些材料有助于将测试与执行过程脱钩。

除此之外,我建议(如法比奥所说)使用工厂女孩。

你可以查看我的轨迹样本项目。 您可以找到 : < a href="https://github.com/lucassus/locomotive" rel="no follow" >https://github.com/lucassus/locomotive

  • how to use factory_girl
  • some examples of custom matchers and macros (in spec/support)
  • how to use shared_examples
  • and finally how to use very nice shoulda-macros

我将使用工厂女孩和Rspecs let方法的组合:

describe User do
  let(:user) { create :user } #  create  is a factory_girl method, that will save a new user in the test database

  it "should be able to run" do
    user.run.should be_true
  end

  it "should not be able to walk" do
    user.walk.should be_false
  end
end


# spec/factories/users.rb
FactoryGirl.define do
  factory :user do
    email { Faker::Internet.email }
    username { Faker::Internet.user_name }
  end
end

这可以让你做这样伟大的事情:

describe User do
  let(:user) { create :user, attributes }
  let(:attributes) { Hash.new }

  it "should be able to run" do
    user.run.should be_true
  end

  it "should not be able to walk" do
    user.walk.should be_false
  end

  context "when user is admin" do
    let(:attributes) { { admin: true } }
    it "should be able to walk" do
      user.walk.should be_true
    end
  end
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签