rails rspec before all vs before each [英] rails rspec before all vs before each

查看:34
本文介绍了rails rspec before all vs before each的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

contest_entry_spec.rb

    require 'spec_helper'

    describe ContestEntry do

      before(:all) do
        @admission=Factory(:project_admission)
        @project=Factory(:project_started, :project_type => @admission.project_type)
        @creative=Factory(:approved_creative, :creative_category => @admission.creative_category)
        @contest_entry=Factory(:contest_entry, :design_file_name => 'bla bla bla', :owner => @creative, :project => @project)
      end

      context 'non-specific tests' do
        subject { @contest_entry }
        it { should belong_to(:owner).class_name('User') }
        it { should belong_to(:project) }
        it { should have_many(:entry_comments) }

        it { should validate_presence_of(:owner) }
        it { should validate_presence_of(:project) }
        it { should validate_presence_of(:entry_no) }
        it { should validate_presence_of(:title) }

      end
end

当我运行这些测试时,一切正常,但如果我将 before(:all) 更改为 before(:each),每个测试都会失败.我不知道为什么会这样?

When I run these tests everything is okey but if I change before(:all) to before(:each) every test will be failed.I don't know why it happens?

这是错误

 Failure/Error: @contest_entry=Factory(:contest_entry, :design_file_name => 'bla bla bla', :owner => @creative, :project => @project)
     ActiveRecord::RecordInvalid:
       Validation Failed: User is not allowed for this type of project

推荐答案

before(:all) 在所有示例运行之前运行该块一次.

before(:all) runs the block one time before all of the examples are run.

before(:each) 在文件中的每个规范之前运行该块一次

before(:each) runs the block one time before each of your specs in the file

before(:all) 设置实例变量 @admission, @project, @creative, @contest_entry 在所有 it 之前一次> 块运行.

before(:all) sets the instance variables @admission, @project, @creative, @contest_entry one time before all of the it blocks are run.

然而,每次 it 块运行时,:before(:each) 都会重置 before 块中的实例变量.

However, :before(:each) resets the instance variables in the before block every time an it block is run.

这是一个微妙的区别但很重要

Its a subtle distinction but important

再说一遍

before(:all)
#before block is run
it { should belong_to(:owner).class_name('User') }
it { should belong_to(:project) }
it { should have_many(:entry_comments) }

it { should validate_presence_of(:owner) }
it { should validate_presence_of(:project) }
it { should validate_presence_of(:entry_no) }
it { should validate_presence_of(:title) }

before(:each)
# before block
it { should belong_to(:owner).class_name('User') }
# before block
it { should belong_to(:project) }
# before block
it { should have_many(:entry_comments) }
# before block

# before block
it { should validate_presence_of(:owner) }
# before block
it { should validate_presence_of(:project) }
# before block
it { should validate_presence_of(:entry_no) }
# before block
it { should validate_presence_of(:title) }

这篇关于rails rspec before all vs before each的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆