<commit_before># Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2013-14 Opscode, Inc.

require 'spec_helper'
require 'chef/knife/cloud/list_resource_command'
require 'support/shared_examples_for_command'

describe Chef::Knife::Cloud::ResourceListCommand do
  it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::ResourceListCommand.new
  
  let (:instance) {Chef::Knife::Cloud::ResourceListCommand.new}
  let (:resources) {[ TestResource.new({:id => "resource-1", :os => "ubuntu"}),
                   TestResource.new({:id => "resource-2", :os => "windows"})]}

  context "Basic tests:" do
    it "raises exception to override #query_resource method" do
      instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
      expect {instance.run}.to raise_error(Chef::Exceptions::Override, "You must override query_resource in #{instance.to_s} to return resources.")
    end

    it "responds to #list method" do
      instance.stub(:query_resource)
      instance.should respond_to(:list)
    end

    context "responds to #list method" do
      let(:test_resource) { "test" }
      before(:each) do
        instance.stub_chain(:ui, :fatal)
      end

      it "handle generic exception" do
        test_resource.stub(:sort_by).and_raise StandardError
        expect {instance.list(test_resource)}.to raise_error(StandardError)
      end

      it "handle Excon::Errors::BadRequest exception." do
        test_resource.stub(:sort_by).and_raise Excon::Errors::BadRequest.new("excon error message")
        expect {instance.list(test_resource)}.to raise_error(Excon::Errors::BadRequest)
      end
    end
  end

  context "Without columns_with_info parameter in #list:" do
    before do
      instance.stub(:query_resource).and_return(resources)
      instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
      instance.stub(:puts)
    end

    it "lists resources in json format when columns_with_info parameter is empty" do
      instance.should_receive(:puts).with(resources[0].to_json)
      instance.should_receive(:puts).with(resources[1].to_json)
      instance.should_receive(:puts).with("\n").twice
      instance.run
    end<commit_msg>Fixed the spec to set expectation when the error actually occurs<commit_after># Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2013-14 Opscode, Inc.

require 'spec_helper'
require 'chef/knife/cloud/list_resource_command'
require 'support/shared_examples_for_command'

describe Chef::Knife::Cloud::ResourceListCommand do
  it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::ResourceListCommand.new
  
  let (:instance) {Chef::Knife::Cloud::ResourceListCommand.new}
  let (:resources) {[ TestResource.new({:id => "resource-1", :os => "ubuntu"}),
                   TestResource.new({:id => "resource-2", :os => "windows"})]}

  context "Basic tests:" do
    it "raises exception to override #query_resource method" do
      instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
      expect {instance.run}.to raise_error(Chef::Exceptions::Override, "You must override query_resource in #{instance.to_s} to return resources.")
    end

    it "responds to #list method" do
      instance.stub(:query_resource)
      instance.should respond_to(:list)
    end

    context "responds to #list method" do
      let(:test_resource) { "test" }
      before(:each) do
        instance.ui.should_receive(:fatal)
      end

      it "handle generic exception" do
        test_resource.stub(:sort_by).and_raise StandardError
        expect {instance.list(test_resource)}.to raise_error(StandardError)
      end

      it "handle Excon::Errors::BadRequest exception." do
        test_resource.stub(:sort_by).and_raise Excon::Errors::BadRequest.new("excon error message")
        expect {instance.list(test_resource)}.to raise_error(Excon::Errors::BadRequest)
      end
    end
  end

  context "Without columns_with_info parameter in #list:" do
    before do
      instance.stub(:query_resource).and_return(resources)
      instance.stub(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new)
      instance.stub(:puts)
    end

    it "lists resources in json format when columns_with_info parameter is empty" do
      instance.should_receive(:puts).with(resources[0].to_json)
      instance.should_receive(:puts).with(resources[1].to_json)
      instance.should_receive(:puts).with("\n").twice
      instance.run
    end
{'fraction_non_alphanumeric': 0.12963369159903487, 'fraction_numerical': 0.004386926957666155, 'mean_word_length': 4.223367697594502, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1773939', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1374, 'n_words': 294}