【问题标题】:Spec for File creation and writing into File文件创建和写入文件的规范
【发布时间】:2012-02-21 15:36:47
【问题描述】:

我是新来的。我正在做一个带有一些测试的项目。我在为课程编写规范时遇到了一些问题。我完成了一些简单的规格,但我不知道如何为这个写。任何帮助将不胜感激。

我的班级

Class Writer

def initialize(filepath)
  @filepath = RAILS_ROOT + filepath
  @xml_document = Nokogiri::XML::Document.new
end

def open
  File.open(@filepath,"w") do |f|
    @gz = Zlib::GzipWriter.new(f)
    @gz.write(%[<?xml version="1.0" encoding="UTF-8"?>\n])
    @gz.write(%[<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n])
    yield self
    @gz.write(%[</urlset>])
    @gz.close
  end
end

def write_entry_to_xml(entry)
   node = Nokogiri::XML::Node.new( "url" , @xml_document )
   node["loc"] = entry.loc
   node["changefreq"] = entry.changfreq
   node["priority"] = entry.priority
   node["lastmod"] = entry.lastmod
   @gz.write(node.to_xml)
end

end

目前我写的如下

describe "writer" do 
 before :each do 
  @time = Time.now
  @filepath = RAILS_ROOT + "/public/sitemap/test/sitemap_test.xml.gz"
  File.open(@filepath,"w") do |f|
   @gz = Zlib::GzipWriter.new(f)
  end
  @xml_document = Nokogiri::XML::Document.new
  @entry = Sitemap::Entry.new("location", "monthly", "0.8", @time)
end 

describe "open" do 
 it "should create a file and write xml entries to it" do 
 end
end

describe "write_entry_to_xml" do 
 it "should format and entry to xml node and write it" do 
  node = Nokogiri::XML::Node.new( "url" , @xml_document )
  node["loc"].should == @entry.loc
  node["changefreq"].should == @entry.changfreq
  node["priority"].should == @entry.priority
  node["lastmod"].shoul == @entry.lastmod
end

结束

谁能帮我写出这门课的完整规范。 提前致谢

【问题讨论】:

  • 您没有复制/粘贴代码吗? Class, shoul?
  • 复制/粘贴代码是什么意思?你的意思是class code和spec code是一样的吗?
  • 测试意味着测试你的实际代码,而不是你真实代码内容的副本。您应该了解测试替身以了解如何测试真实代码的特定语句
  • 你能指导我这个具体的课程吗
  • @farnoy 你能帮我解决这个问题吗?

标签: ruby-on-rails ruby rspec rspec2


【解决方案1】:

我没有时间为你做这一切,但这里是我如何测试我的代码的示例:

请注意:Ropet::Config.expects(:new).returns(config),这可用于您的Nokogiri::XML::Node#new

我的规范使用 RSpec 和 Mocha,我喜欢这种设置的简单性以及使用这些简单工具可以完成的工作。

编辑:粗略规格

def write_entry_to_xml(entry)
   node = Nokogiri::XML::Node.new( "url" , @xml_document )
   node["loc"] = entry.loc
   node["changefreq"] = entry.changfreq
   node["priority"] = entry.priority
   node["lastmod"] = entry.lastmod
   @gz.write(node.to_xml)
end

可能是这样的,虽然我不知道你的代码的目的。

it 'writes entry to xml' do
    content = double('output')
    node = double('node'); node.should_receive(:to_xml).and_return(content);
    gz = double('gz'); gz.should_receive(:write).with(content)
    w = Writer.new("some_path"); w.open
    w.instance_variable_set(:@gz, gz) # i'm guessing @gz is assigned after open only?
    entry = # i don't know what entry is
    Nokogiri::XML::Node.stub(:new).and_return(node)
    node.should_receive(:[]).with("loc", entry.loc)
    node.should_receive(:[]).with("changefreq", entry.changefreq)
    node.should_receive(:[]).with("priority", entry.priority)
    node.should_receive(:[]).with("lastmod", entry.lastmod)
    w.write_entry_to_xml(entry)
end

【讨论】:

  • 你能给我提供一些关于这个类的粗略的规格草图吗?
  • 好的,我已经这样做了,但我不知道它是否适合您的情况,因为我不知道它的作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 2011-12-11
  • 2014-10-07
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多