我是rails的新手,刚开始使用rspec版本编写测试:2.11.1。我正在寻找一种方法为我班上的不同测试种下不同的数据。为此,我在测试中创建了一个静态函数。根据我要求实例化不同数量的对象。我有一个函数seed_data,它根据传递给它的数字实例化不同数量的对象。我得到了这个例外:
NoMethodError: undefined method `seed_data' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x007fcedd9bea70>
请查看以下示例代码:
require 'spec_helper'
require 'go_live_sale'
require 'rspec/expectations'
describe GoLiveSale do
context "get_go_live_sale_for_sale_ids Function Correctness" do
describe "Testing get_go_live_sale_for_sale_ids() function correctness " do
it "should return error when sale_ids is blank" do
result = GoLiveSale.get_go_live_sale_for_sale_ids({})
result[:err].should_not be_blank
result[:err].should == "err1"
end
it "should return all columns corresponding to select field for single go_live_sale" do
go_live_sales = seed_data(1)
sale_ids = go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
result = GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids})
result[:err].should be_blank
result[:go_live_sales].count.should_be == 1
delete_seed_data(go_live_sales)
end
it "should return all columns corresponding to select field for multiple go_live_sale" do
go_live_sales = seed_data(2)
sale_ids = go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids})
result[:err].should be_blank
result[:message].should be_blank
result[:go_live_sales].count.should == 2
delete_seed_data(go_live_sales)
end
it "should return selected columns corresponding to select field for single go_live_sale" do
go_live_sales = seed_data(1)
sale_ids = go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
result = GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids, :columns => ["id","sale_id"]})
result[:err].should be_blank
result[:go_live_sales].count.should_be == 1
delete_seed_data(go_live_sales)
end
it "should return selected columns corresponding to select field for multiple go_live_sale" do
go_live_sales = seed_data(2)
sale_ids = go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids, :columns => ["id","sale_id"]})
result[:err].should be_blank
result[:message].should be_blank
result[:go_live_sales].count.should == 2
delete_seed_data(go_live_sales)
end
it "should return error when selecting erroneous columns" do
go_live_sales = seed_data(2)
sale_ids = go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids, :columns => ["id","random_sale_id"]})
result[:err].should_not be_blank
delete_seed_data(go_live_sales)
end
end
end
end
def self.delete_seed_data(go_live_sales)
go_live_sales.each do |go_live_sale|
go_live_sale.delete
end
end
def self.seed_data(number_of_go_live_sale_to_create)
go_live_sales =[]
(1..number_of_go_live_sale_to_create).each do |number|
go_live_sales.push(create_go_live_sale(number))
end
return go_live_sales
end
def self.create_go_live_sale(number_to_add)
go_live_sale = GoLiveSale.new
go_live_sale.start_date = Time.now
go_live_sale.sale_id = Sale.select("IFNULL(max(id),0)+#{number_to_add} as sale_id").first.try(:sale_id)
go_live_sale.sale_name = "Test Sale" + go_live_sale.sale_id.to_s
go_live_sale.sale_type = "Flash Sale"+ go_live_sale.sale_id.to_s
User.current_user = User.first
go_live_sale.save
return go_live_sale
end
RSpec::Matchers.define :be_valid do
match_for_should do |actual|
actual[:err].blank?
actual[:validation_error].blank?
actual[:is_valid] == true
end
match_for_should_not do |actual|
actual[:err].present?
actual[:validation_error].present?
actual[:is_valid] == false
end
failure_message_for_should do |actual|
"Expected validation to pass, but it failed"
end
failure_message_for_should_not do |actual|
"Expected validation to fail, but it passed"
end
end
我知道这是一个范围问题,或者rspec不允许你以这种方式编写测试。如果有人可以编写一小段代码来解释如何在这种情况下实例化测试数据,那将会很棒。我的rails版本是3.0.5。
答案 0 :(得分:1)
您可以删除方法定义中的self.
并将其移至describe GoLiveSale do
块内,然后按预期使用seed_data
调用它。
例如:
describe GoLiveSale do
def my_method
end
context "some context" do
it "should call my_method" do
expect {my_method}.not_to raise_error
end
end
end
此规范应该通过。