<?xml version="1.0" encoding="UTF-8"?>
<codes type="array">
  <code>
    <code>using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestingIntoSingleton
{
    [TestClass]
    public class TestActiveUsers
    {
        [TestMethod]
        public void Should_List_Logged_In_Users()
        {
            ActiveUsers UserManager = ActiveUsers.GetActiveUsers();
            UserManager.Add("Brandon");
            UserManager.Add("Amy");
            UserManager.Add("Jack");
            
            List&lt;string&gt; ExpectedUsers = new List&lt;string&gt;();
            ExpectedUsers.Add("Brandon");
            ExpectedUsers.Add("Amy");
            ExpectedUsers.Add("Jack");            

            foreach (var ExpectedUser in ExpectedUsers)
            {
                Assert.IsTrue( UserManager.Users.Contains(ExpectedUser));    
            }
        }

        [TestMethod]
        public void Should_Have_Only_One_Instance_Of_ActiveUsers()
        {
            ActiveUsers UserManager1 = ActiveUsers.GetActiveUsers();
            ActiveUsers UserManager2 = ActiveUsers.GetActiveUsers();
         
            UserManager1.Add("User");
            Assert.IsTrue(UserManager2.Users.Contains("User"));
        }

        [TestMethod]
        public void Should_Not_Allow_New_Instances()
        {
            try
            {
                ActiveUsers UserManager = new ActiveUsers();
                //We should not get this far
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.AreEqual("This object must be instantiated by the GetActiveUsers method", ex.Message);                 
            }
        }
    }

    public class ActiveUsers
    {
        private static ActiveUsers _instance;
        public List&lt;string&gt; Users;

        public ActiveUsers()
        {
            throw new System.Exception("This object must be instantiated by the GetActiveUsers method");            
        }

        private ActiveUsers(bool YouAreASingleton)
        {
            Users = new List&lt;string&gt;();
        }

        public static ActiveUsers GetActiveUsers()
        {        
            if (_instance == null)
            {
                _instance = new ActiveUsers(true);
            }
            return _instance;
        }

        public void Add(string User)
        {
            Users.Add(User);
        }
    }   
}</code>
    <comment>I am doing a step by step example of TDD'ing towards the singleton pattern.  The step by step Red, Green, Refactor cycle is at http://blog.sonerdy.com/2009/02/testing-into-singleton-pattern.html  I am wondering if anyone has a more elegant way of testing that the object can only be instantiated by the factory method.  This example uses an assertion that expects an exception when trying to new up the object the normal way.  I haven't gone as far as to make sure this is thread safe.  It's more of an excercise in Test-Driven development, and working towards a pattern.  The idea of the ActiveUsers class is that it will have a list of the current users of the program.  It's just a simple problem.  </comment>
    <created-at type="datetime">2009-02-09T17:31:13+00:00</created-at>
    <id type="integer">750</id>
    <language>C#</language>
    <permalink>testing-to-the-singleton-pattern</permalink>
    <refactors-count type="integer">5</refactors-count>
    <title>Testing the Singleton Pattern</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-03-14T00:48:40+00:00</updated-at>
    <user-id type="integer">1287</user-id>
    <user>
      <id type="integer">1287</id>
      <identity-url>http://www.sonerdy.com</identity-url>
      <name>Brandon Joyce</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">3</refactors-count>
      <website>http://blog.sonerdy.com</website>
    </user>
  </code>
  <code>
    <code>require 'erb'

# GOAL: render the template with the following vars
vars = { :first =&gt; 'Mislav', 'last' =&gt; 'Marohnic' }
template = 'Name: &lt;%= first %&gt; &lt;%= last %&gt;'

# create anonymous module which binding we will use
m = Module.new do
  # I want the singleton class, thank you
  class &lt;&lt; self
    public :binding
    
    def meta
      class &lt;&lt; self; self; end
    end
  end

  class &lt;&lt; meta
    public :define_method
  end

  # define singleton method for each var
  vars.each do |name, value|
    meta.define_method(name) { value }
  end
end

puts ERB.new(template).result(m.binding)
#=&gt; "Name: Mislav Marohnic"
</code>
    <comment>This works. Now, is there a simpler way than my solution?</comment>
    <created-at type="datetime">2008-04-12T15:12:14+00:00</created-at>
    <id type="integer">281</id>
    <language>Ruby</language>
    <permalink>given-a-hash-of-variables-render-an-erb-template</permalink>
    <refactors-count type="integer">3</refactors-count>
    <title>Given a hash of variables, render an ERB template</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-04-12T17:34:10+00:00</updated-at>
    <user-id type="integer">548</user-id>
    <user>
      <id type="integer">548</id>
      <identity-url>http://mislav.caboo.se</identity-url>
      <name>mislav</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website>http://mislav.caboo.se/</website>
    </user>
  </code>
</codes>
