Tests Everywhere - Ruby
Tags: test, ruby, rspec, tutorial
Published: 2023-11-23 00:19:46

{% github rogervinas/tests-everywhere no-readme %}

[Ruby](https://www.ruby-lang.org) testing [this simple Hello World](https://dev.to/rogervinas/tests-tests-everywhere-1b6h) with [RSpec](https://rspec.info/)

- [Show me the code](#show-me-the-code)
  - [Implementation](#implementation)
  - [Test](#test)
- [Run this project using 🐳 docker](https://github.com/rogervinas/tests-everywhere/tree/main/ruby#run-this-project-using--docker)
- [Run this project locally](https://github.com/rogervinas/tests-everywhere/tree/main/ruby#run-this-project-locally)

## Show me the code

### Implementation

1. Create `HelloMessage` class in [HelloMessage.rb](https://github.com/rogervinas/tests-everywhere/blob/main/ruby/lib/HelloMessage.rb):

```ruby
class HelloMessage
  def text
    @text = "Hello World!"
  end
end
```

2. Create `HelloConsole` class in [HelloConsole.rb](https://github.com/rogervinas/tests-everywhere/blob/main/ruby/lib/HelloConsole.rb):

```ruby
class HelloConsole
  def print(text)
    puts text
  end
end
```

3. Create `HelloApp` class in [HelloApp.rb](https://github.com/rogervinas/tests-everywhere/blob/main/ruby/lib/HelloApp.rb):

```ruby
class HelloApp
  def initialize(message, console)
    @message = message
    @console = console
  end

  def printHello
    @console.print(@message.text)
  end
end
```

4. Create main function in [Main.rb](https://github.com/rogervinas/tests-everywhere/blob/main/ruby/Main.rb) that wraps it all together:

```ruby
message = HelloMessage.new
console = HelloConsole.new
app = HelloApp.new message, console
app.printHello
```

### Test

Following [RSpec Core](https://rspec.info/features/3-12/rspec-core/) and [RSpec Mocks](https://rspec.info/features/3-12/rspec-mocks/) guides ...

1. Test `HelloMessage` in [HelloMessage_spec.rb](https://github.com/rogervinas/tests-everywhere/blob/main/ruby/spec/HelloMessage_spec.rb):

```ruby
RSpec.describe HelloMessage do
  it "should return hello world" do
    message = HelloMessage.new
    expect(message.text).to eq("Hello World!")
  end
end
```

2. Test `HelloApp` in [HelloApp_spec.rb](https://github.com/rogervinas/tests-everywhere/blob/main/ruby/spec/HelloApp_spec.rb):

```ruby
RSpec.describe HelloApp do
  it "should print hello message" do
    messageText = "Hello Test!"

    # 2.1 Create a mock of HelloMessage
    # that will return "Hello Test!" whenever text is called
    message = instance_double("HelloMessage", :text => messageText)

    # 2.2 Create a mock of HelloConsole
    console = instance_double("HelloConsole")
    # 2.3 Expect print to be called once with "Hello Test!"
    expect(console).to receive(:print).once.with(messageText)

    # 2.4 Create a HelloApp, the one we want to test, passing the mocks
    app = HelloApp.new message, console
    # 2.5 Execute the method we want to test
    app.printHello

    # Expectation 2.3 will be checked once test ends
  end
end
```

3. Test output should look like:

```
HelloApp
  should print hello message

HelloMessage
  should return hello world

Finished in 0.00457 seconds (files took 0.05704 seconds to load)
2 examples, 0 failures
```

Happy Testing! 💙