rspect समस्याओं - रूबी, आरएसपीईसी

यहां कुछ कोड है जो मुझे काम करता है:

require "csv"

class Motorcycle
attr_reader :name, :weight
@@count = 0

def self.find (name)
found = nil
ObjectSpace.each_object(Motorcycle) { |o|
found = o if o.name == name
}
return found
end

def self.create
File.new("motorcycles.csv").readlines[1..-1].map{ |line|
Motorcycle.new( *line.split( "," )  )
}
end

def initialize (name, weight)
@name = name
@weight = weight
self.class.count += 1
end

def self.count
return @@count
end

def self.count=( count )
@@count = count
end

def available_colors

colors=[]
colorsFile = File.read("colors.csv").split("n").map { |line| line.split(",") }
for i in (0..colorsFile.flatten.length) do
if (colorsFile.flatten[i].to_s == self.name.to_s)
colors.push(colorsFile.flatten[i+1])
end
end

return colors
end

def contains (name,color)
if(self.name.to_s == name)
else
return color
end
end

def has_abs?
File.open( "abs.txt" ) do |io|
io.each {|line| line.chomp! ; return true if line.include? self.name.to_s}
end

return false
end

end

Motorcycle.create

कोड को इस परीक्षण को आरएसपीईसी पर पास करना होगा:

describe Motorcycle do
describe "loading the motorcycle list" do
it "should load 2 motorcycles from the CSV" do
Motorcycle.count.should == 2
end
end

describe "finding a motorcycle by name" do
it "should return an instance of the Motorcycle class" do
Motorcycle.find("1200 RT").should be_a Motorcycle
end
end

describe "#weight" do
it "should have a weight of 800 pounds for the 1200 RT" do
Motorcycle.find("1200 RT").weight.should == "800 pounds"
end

it "should have a weight of 500 pounds for the 600 GS" do
Motorcycle.find("600 GS").weight.should == "500 pounds"
end
end

describe "#available colors" do
it "should find "red" and "black" as available colors for the BMW 1200 RT" do
Motorcycle.find("1200 RT").available_colors.should == [ "red", "black" ]
end

it "should find "green" and "blue" as available colors for the BMW 600 GS" do
Motorcycle.find("600 GS").available_colors.should == [ "green", "blue" ]
end
end

describe "#has_abs?" do
it "should be true for a motorcycle that appears in abs_motorcycles.txt" do
Motorcycle.find("1200 RT").has_abs?.should be_true
end

it "should be false for a motorcycle that does not appear in abs_motorcycles.txt" do
Motorcycle.find("600 GS").has_abs?.should be_false
end
end
end

समस्या यह है कि, पहले परीक्षण के बाद (जहां यह मोटोक्रिकल उदाहरणों की मात्रा की गणना करता है) प्रत्येक उदाहरण एक शून्य है, यह नहीं कहता है कि मुट्ठी को छोड़कर हर परीक्षण विफल हो जाता है। आउटपुट लॉग यहां है:

Failures:

1) Motorcycle finding a motorcycle by name should return an instance of the Motorcycle class
Failure/Error: Unable to find matching line from backtrace
expected nil to be a kind of Motorcycle
# ./motoapp.rb:76

2) Motorcycle#weight should have a weight of 800 pounds for the 1200 RT
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `weight" for nil:NilClass
# ./motoapp.rb:82

3) Motorcycle#weight should have a weight of 500 pounds for the 600 GS
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `weight" for nil:NilClass
# ./motoapp.rb:86

4) Motorcycle#available colors should find "red" and "black" as available colors for the BMW 1200 RT
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `available_colors" for nil:NilClass
# ./motoapp.rb:92

5) Motorcycle#available colors should find "green" and "blue" as available colors for the BMW 600 GS
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `available_colors" for nil:NilClass
# ./motoapp.rb:96

6) Motorcycle#has_abs? should be true for a motorcycle that appears in abs_motorcycles.txt
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `has_abs?" for nil:NilClass
# ./motoapp.rb:102

7) Motorcycle#has_abs? should be false for a motorcycle that does not appear in abs_motorcycles.txt
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `has_abs?" for nil:NilClass
# ./motoapp.rb:106

Finished in 0.01223 seconds
8     examples, 7 failures

मैं यह सोचने के इच्छुक हूं कि यह मेरे परिणाम के कारण मैन्युअल परीक्षण करने के कारण किसी प्रकार की बग या कुछ है:

puts Motorcycle.count
puts Motorcycle.find("1200 RT")
puts Motorcycle.find("1200 RT").weight
puts Motorcycle.find("600 GS").weight
puts Motorcycle.find("1200 RT").available_colors
puts Motorcycle.find("600 GS").available_colors
puts Motorcycle.find("1200 RT").has_abs?
puts Motorcycle.find("600 GS").has_abs?

जो मुझे यह आउटपुट देता है:

2
#<Motorcycle:0x7fd8bffcfd88>
800 pounds
500 pounds
red
black
green
blue
true
false

इसलिए मैं वास्तव में एक मृत अंत में बहुत अधिक हूं, ¿क्या किसी के पास यह पता चल सकता है कि क्या हो रहा है?

उत्तर:

जवाब के लिए 2 № 1

ऐसा लगता है कि आप मोटरसाइकिल संग्रहित नहीं कर रहे हैंनिर्माण कॉल करते समय कहीं भी ऑब्जेक्ट्स, इसलिए यदि जीसी को इंस्टेंस कहा जाता है तो बस चलेगा। शायद अधिक रूबी कोड (आरएसपीईसी) के भार लोड करना जीसी के जल्द ही होने का आह्वान करता है।

आप केवल कक्षा के स्तर में उदाहरणों को क्यों नहीं स्टोर करते हैं Set? (उदाहरण के साथ गिनती कॉल को बदलना instances << self) या Hash(उदाहरण के रूप में उनके नाम के साथ उदाहरण भंडार)

यह भी मुझे लगता है कि आप निष्पादन के आरएसपीईसी आदेश पर भरोसा कर रहे हैं जब आपके पास होना चाहिए before ब्लॉक जिसमें आप सीएसवी से पढ़ते हैं।

उदाहरण कोड:

Class Motorcycle
# use a constant, this will always point to the same object even though
# the content of the object changes.
# Using @@instances would also be ok
Instances = Set.new # or you can use Instances = Hash.new
def initialize (name, weight)
@name = name
@weight = weight
#
Instances << self # or store the name/instance mapping Instances[name] = self
end