简体   繁体   English

Ruby on Rails 2.3.8:测试:如何设置实例变量以在整个测试中使用?

[英]Ruby on Rails 2.3.8: Testing: How do I set up an instance variable to use throughout my tests?

Lets say I have some data that remanis the same throughout all of my tests, for forever and eternity. 可以说,我拥有一些数据,这些数据在我所有的测试中都存在,直到永远。 I create this data in setup . 我在setup创建此数据。 I store the data to @instance_var . 我将数据存储到@instance_var But when I call @instance_var.attribute in any test, I get the following error: 但是在任何测试中调用@ instance_var.attribute时,都会出现以下错误:

RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

I know my instance variable isn't null, cause after it is set, I can do a puts @instance_var.inspect on it... 我知道我的实例变量不为null,因为在设置它之后,我可以在其上执行puts @ instance_var.inspect ...

Any ideas? 有任何想法吗?

EDIT: 编辑:

 setup do
    user = Factory(:user)
    account = Factory(:account)    

    set_user(user)
    set_account(account)


    puts "||||||||||||||||||||||||||||||||||||||||||||||" #this proves that the instance vars are not null
    puts "| object_test.rb                            |"
    puts "|      #{@user.name}                   "
    puts "|      #{@account.name}                "
    puts "||||||||||||||||||||||||||||||||||||||||||||||"
  end

A failing test (with the error above) 测试失败(出现上述错误)

test "for detection of first content with multiple contents" do
      object = Factory(:object, :user_id => @user.id, :account_id => @account.id)
   ... #the rest of this test isn't important, as it is the above line, on @user, where the nil.id error occers

in test_helper.rb 在test_helper.rb中

def set_user(user)
  @user = user
end

def set_account(account)
  @account = account
end

I don't really think I need these two methods, as when I define the @instance variable in setup, I get teh same result 我真的不认为我需要这两种方法,因为当我在安装程序中定义@instance变量时,会得到相同的结果

in test_helper.rb there are some constants set fore ActiveSupport::TestCase: 在test_helper.rb中,为ActiveSupport :: TestCase设置了一些常量:

  self.use_transactional_fixtures = true

  self.use_instantiated_fixtures  = false

  fixtures :all

disabling these did nothing. 禁用这些功能无济于事。 =( =(

Have you tried 你有没有尝试过

setup do
  @user = Factory(:user)
  @account = Factory(:account)
end

Normally, if you set the instance variables in the setup block, they should be available to all your tests. 通常,如果您在设置块中设置实例变量,则它们应可用于所有测试。 (You might be having an issue with scopes.) (您可能对合并范围有疑问。)

My solution was to make a shared class, shared_test.rb 我的解决方案是创建一个共享类shared_test.rb

require 'test_helper'

class SharedTest
  def self.initialize_testing_data
    self.reset_the_database

    self.set_up_user_and_account
    # make sure our user and account got created 
    puts "|||||||||||||||||||||||||||||||||||||||||||||"
    puts "| The user and account "
    puts "| we'll be testing with:"
    puts "|             #{@user.name}"
    puts "|             #{@user.account.name}"
    puts "|||||||||||||||||||||||||||||||||||||||||||||"
  end

  def self.reset_the_database
    #clear the database and reset it
    call_rake("db:test:prepare")
    call_rake("db:bootstrap RAILS_ENV=test")
  end

  def self.set_up_user_and_account
    #set up our user for doing all our tests (this person is very busy)  
    @user = Factory(:user)
    @account = Factory(:account)    
    @user.account = @account
    @user.save
  end
end

so then at the top of every test file that needs user and account to stay the same between all the tests, you just do 因此,在需要用户和帐户在所有测试之间保持一致的每个测试文件的顶部,您只需执行

require 'shared_test.rb'

and methods are called like 和方法称为

SharedTest.initialize_testing_data 

暂无
暂无

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

相关问题 Ruby on Rails 2.3.8:如何设置我的单元测试,以便在出现错误时,我可以打印当前范围内的所有变量? - Ruby on Rails 2.3.8: How do I set up my unit tests such that when there is an error, I can have all the variables in the current scope printed? Ruby on Rails 2.3.8:测试:实例变量Nil,当在类顶部声明时? - Ruby on Rails 2.3.8: Testing: Instance Variable Nil, when declared at top of class? 如何在Ruby on Rails 2.3.8中获得选定的值? - How do I obtain a selected value in Ruby on Rails 2.3.8? Ruby on Rails:2.3.8:如何使searchlogic与bundler一起使用? - Ruby on Rails: 2.3.8: How do I get searchlogic to work with bundler? Ruby on Rails 2.3.8:我如何需要特定版本的gem? - Ruby on Rails 2.3.8: How do I require a specifc version of a gem? 如何在heroku上使用具有声明授权的Rails 2.3.8? - How do I use rails 2.3.8 with declarative authorization on heroku? Ruby on Rails 2.3.8:Factory_girl:全局变量不能在整个工厂构建过程中保持价值吗? - Ruby on Rails 2.3.8: Factory_girl: Global Variable doesn't maintain value throughout factory builds? Ruby on Rails 2.3.8:环境定义了用于生产和开发的所有gem,如何指定测试gem? - Ruby on Rails 2.3.8: Environment defines all the gems used for production and development, how do you specify testing gems? 如何在Ruby on Rails中使用带有邮件程序的实例变量? - How do you use an instance variable with mailer in Ruby on Rails? 如何从Rails 2.3.8应用程序的Ruby 1.8.7升级到Ruby 1.9.2? - How do I upgrade from Ruby 1.8.7 to Ruby 1.9.2 for Rails 2.3.8 app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM