代码之家  ›  专栏  ›  技术社区  ›  coreypurcell

使用expect.进行更改,不会传递,但在控制台中有效

  •  0
  • coreypurcell  · 技术社区  · 14 年前

    我想在我的模型上测试一种方法。在控制台中运行可以,但是我的rspec测试没有通过。签出代码:

    模型:

    def import(master)
      hsh = master.attributes.delete_if { |k,v| k == 'id' }
      self.update_attributes(hsh)
    end
    

    RSPEC测试:

    describe "#import" do
    
      let(:master) { Factory(:sheet, :work_order => 'M1234', :sample_size => 10, :sample_scheme => 'TEST#') }
      let(:wo) { Factory(:sheet, :work_order => 'W1234', :sample_scheme => 'ORIG#' ) }
    
      it "imports all of the attributes from the master" do
        expect { wo.import(master) }.to change( wo, :sample_scheme ).to(master.sample_scheme)
      end
    
    end
    

    我想不出来,这里是输出:

    'Sheet#import imports all of the attributes from the master' FAILED
    sample_scheme should have been changed to "TEST#", but is now "ORIG#"
    

    如我所说,当在控制台中运行时,代码正确地从主服务器导入属性。只是RSPEC测试失败了。我做错什么了?


    更改为导入函数将导致传递:

    def import(master)
      hsh = master.attributes.delete_if { |k,v| k == 'id' }
      hsh.each do |k,v|
        self.update_attribute(k, v)
      end
      #self.update_attribute(:sample_scheme, hsh['sample_scheme'])
      #self.update_attributes(hsh)
    end
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   shingara    14 年前

    describe "#import" do
    
      let(:master) { Factory(:sheet, :work_order => 'M1234', :sample_size => 10, :sample_scheme => 'TEST#') }
      let(:wo) { Factory(:sheet, :work_order => 'W1234', :sample_scheme => 'ORIG#' ) }
    
      it "imports all of the attributes from the master" do
        wo.sample_scheme.should.not == master.sample_scheme
        wo.import(master)
        wo.sample_scheme.should == master.sample_scheme
      end
    
    end
    
        2
  •  0
  •   coreypurcell    14 年前