为什么我不能在我的Rspec类中调用静态函数?



我是rails新手,刚刚开始使用rspec version: 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

您可以在方法定义中去掉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

相关内容

  • 没有找到相关文章

最新更新