|
| 1 | +# Ruby-Lab |
| 2 | + |
| 3 | +- Build the Ruby development environment via **Visual studio code** with extension [Ruby](https://github.com/rubyide/vscode-ruby) |
| 4 | +- For the **BDD** (Behaviour Drive Development) purpose, download and installed [RSpec](http://rspec.info/) |
| 5 | + |
| 6 | +1. First we created empty class called: *Account* inside the account.rb |
| 7 | +```sh |
| 8 | +class Account |
| 9 | +end |
| 10 | +``` |
| 11 | +2. Then we created our first test scenario inside the account_spec.rb |
| 12 | +> The testing target aim for *Account* class |
| 13 | +```sh |
| 14 | +RSpec.describe Account do |
| 15 | +end |
| 16 | +``` |
| 17 | +3. We need initilize our account object under the *constructor* |
| 18 | +> So before we startup our testing case, we need create new account object that passing account identity and balance while account first open |
| 19 | +```sh |
| 20 | +RSpec.describe Account do |
| 21 | + before(:all) do |
| 22 | + @inital_balance = 100_100 |
| 23 | + @test_account = Account.new('TestAccount1', @inital_balance) |
| 24 | + end |
| 25 | +end |
| 26 | +``` |
| 27 | +4. Let's run the test case and you might get red light(or failed) |
| 28 | +> Cause we did not implement constructor inside the account.rb yet |
| 29 | +```sh |
| 30 | +class Account |
| 31 | + def initialize(name, balance = 0) |
| 32 | + @name = name |
| 33 | + @balance = balance |
| 34 | + end |
| 35 | +end |
| 36 | +``` |
| 37 | +5. Now turned back to account_spec.rb and performed the testing, we might get rid of error, means that we got green light |
| 38 | +> For the next step, started to write the first feature of test case, for exmaple: *desposit* |
| 39 | +```sh |
| 40 | +RSpec.describe Account do |
| 41 | + before(:all) do |
| 42 | + @inital_balance = 100_100 |
| 43 | + @test_account = Account.new('TestAccount1', @inital_balance) |
| 44 | + end |
| 45 | + describe ".desposit" do |
| 46 | + context 'When transaction amount <= 0' do |
| 47 | + it 'Should return intput amount [x] invalid' do |
| 48 | + @trans = TransactionItem.new(Time.new, -1, DESPOSIT, 'Desposit invalid test') |
| 49 | + @result = @test_account.desposit(@trans) |
| 50 | + expect(@result).to eq INVALIDINPUT |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | +end |
| 55 | +``` |
| 56 | +6. Again triggered the test case, red light return |
| 57 | +> Go back to account.rb and implement the detail of desposit method and created the new class *TranasctionItem*... |
| 58 | +```sh |
| 59 | +class Account |
| 60 | + #.... |
| 61 | + def desposit(trans) |
| 62 | + if trans.amount <= 0 |
| 63 | + puts "Input amount '#{trans.amount}' invalid" |
| 64 | + return INVALIDINPUT |
| 65 | + end |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +class TransactionItem |
| 70 | + attr_reader :date, :amount, :memo, :code |
| 71 | + |
| 72 | + def initialize(date, amount, code, memo) |
| 73 | + @date = date |
| 74 | + @amount = amount |
| 75 | + @code = code |
| 76 | + @memo = memo |
| 77 | + end |
| 78 | +end |
| 79 | +``` |
| 80 | +7. Tried to finish the test case and let it become green light, iterator the process till the user requriement all meet:) |
| 81 | +> Test case -> red light -> implment detail -> green light -> back to test case |
0 commit comments