Shopify Application Development by Larkin Michael

Shopify Application Development by Larkin Michael

Author:Larkin, Michael
Language: eng
Format: epub, mobi, pdf
Published: 2015-06-17T16:00:00+00:00


3. Create the test in /spec/services/contest_results_spec.rb: describe ContestResults do

context "initialize" do

it "should raise exception if not an array" do expect {ContestResults.new([1])}.to_not raise_error expect {ContestResults.new([1,2,3,4])}.to_not raise_error expect {ContestResults.new(["a","b"])}.to_not raise_error expect {ContestResults.new("")}.to raise_error expect {ContestResults.new(nil)}.to raise_error expect {ContestResults.new([])}.to raise_error expect {ContestResults.new}.to raise_error

end

end context "results" do

it "should return the proper results" do contest_results = ContestResults.new([1,2,3,4]) contest_results.results.should be_a Integer contest_results.results(1).should be_a Integer contest_results.results(2).should be_a Array contest_results.results(0).should be_a Integer contest_results.results(nil).should be_a Integer

contest_results = ContestResults.new(["a","b","c","d"]) contest_results.results.should be_a String

contest_results.results(1).should be_a String contest_results.results(2).should be_a Array contest_results.results(0).should be_a String contest_results.results(nil).should be_a String

end

end

end

4. Now that we have tests, we can fill in the methods we stubbed out earlier with code. As mentioned, we are going to use the Array#sample method and return the results. If we later decide to use a different algorithm to choose a winner, we only need to make the change in one class, and we already have tests in place that will ensure that we don't break existing code elsewhere in the app: class ContestResults

def initialize(array)

raise ArgumentError.new("array is required") if array.blank? @array = array

end

#Picks <count> winners def results(count=1) if count.to_i < 2 @array.sample

else

@array.sample(count) end

end

end



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.