[英]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.