How to write a particular rspec test? Advice needed …

I'm new to rspec and I'm trying understand how to write some particular tests.

Background:

  • I'm testing a Sinatra app
  • That app connects to several databases
  • I've defined the usernames/passwords for these databases as environment variables and I reference these variables in the code.

I have working test, so I know how to test for the existence of the environment variables.

Here's how I did it:

27     it "should know the username for each SUBDOMAIN in the list" do
28       @dm.domains.each do |dom|
29         ENV['C_USERNAME_' + dom].nil?.should eq(false)
30       end
31     end

As I said, the test works. The only problem is, when it fails, its unclear which dom doesn't have the environment variable set. The result of the failed test looks the following:

    dhaskew@Air1:~/code/projects/rules$ rake spec
   /Users/dhaskew/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ./spec/c_spec.rb   ./spec/main_spec.rb ./spec/routes_spec.rb
   ..F*.....*

    Pending:
      My Site Domain Manager should know the password for each SUBDOMAIN in the list
      # Not yet implemented
      # ./spec/c_spec.rb:33
      My Site GET '/rules/SUBDOMAINS' should test all subdomains
      # Not yet implemented
      # ./spec/routes_spec.rb:28

   Failures:

     1) My Site Domain Manager should know the username for each SUBDOMAIN in the list
        Failure/Error: ENV['C_USERNAME_' + dom].nil?.should eq(false)

          expected: false
          got: true

          (compared using ==)
         # ./spec/c_spec.rb:29:in `block (4 levels) in <top (required)>'
         # ./spec/c_spec.rb:28:in `each'
         # ./spec/c_spec.rb:28:in `block (3 levels) in <top (required)>'

     Finished in 0.05095 seconds
     10 examples, 1 failure, 2 pending

    Failed examples:

    rspec ./spec/c_spec.rb:27 # My Site Domain Manager should know the username for each SUBDOMAIN in the list
    rake aborted!

Question: How do I write this test(s) better so that I can see which environment variable isn't defined.

Thanks.

Answers


One easy solution is this: Generate the it tests dynamically. In general, strive for one test per scenario/it block. In this case:

@dm.domains.each do |dom|
  it "should know the username for #{dom} in the list" do
    ENV['C_USERNAME_' + dom].should_not be_nil
  end
end

Update

Here's a more complete example. You need to declare @dm outside of a before block for it to be available.

class DomainManager
  attr_reader :domains

  def initialize(domains)
    @domains = domains
  end
end

describe DomainManager do
  @dm = DomainManager.new( %w(example.com example.org))

  @dm.domains.each do |dom|
    it "should know the username for #{dom} in the list" do
      ENV['C_USERNAME_' + dom].should_not be_nil
    end
  end
end

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.