Loading ...
Sorry, an error occurred while loading the content.

Numpang nanya doong...

Expand Messages
  • Fauzan Qadri
    Sore kak / bang� btw salam kenal yaa semua nyaa� ane lagi belajar2 testing pake rspec nih kak / bang� ane punya contoh kasus kaya gini� ... model/faculty.rb
    Message 1 of 8 , Sep 28, 2013
    • 0 Attachment
      Sore kak / bang…
      btw salam kenal yaa semua nyaa…

      ane lagi belajar2 testing pake rspec nih kak / bang…

      ane punya contoh kasus kaya gini…

      ---------------------------------------------------------------------------------------

      model/faculty.rb
      ----------------------
      class Faculty < ActiveRecord::Base
      validates :name, presence: true, uniqueness: true
      end

      spec/factories.rb
      -----------------------
      FactoryGirl.define do
      factory :faculty do 
       name "Some Faculty Name" 
       description "description for faculty name" 
       end
      end

      spec/models/faculty_spec.rb
      ---------------------------------------
      describe Faculty do
      before {@faculty = FactoryGirl.create(:faculty)}
      subject {@faculty}

      it { should be_valid }

      context "Ketika Nama Fakultas telah ada" do
      before do
      faculty_with_same_name = @...
      faculty_with_same_name.save
      end
      it { should_not be_valid }
      end
      end

      --------------------------------------------------------------------------------------

      pas jalanin dia hasilnya kek gini…


      nah yang ane mau tanyain…

      sebener nya " it { should_not be_valid } " pads context "Ketika Nama Fakultas telah ada" itu nge-test yang mana…?

      ketika di faculty_spec.rb ane ubah jadi…

      ---------------------------------------------------------------------------------------------------------------------------------
      spec/models/faculty_spec.rb
      ---------------------------------------------------------------------------------------------------------------------------------
      describe Faculty do
      before {@faculty = Faculty.new({name: "Some Name", description: "Simple description"})}
      subject {@faculty}

      it { should be_valid }

      context "Ketika Nama Fakultas telah ada" do
      before do
      faculty_with_same_name = @...
      faculty_with_same_name.save
      end
      it { should_not be_valid }
      end
      end
      ---------------------------------------------------------------------------------------------------------------------------------

      hasil test nya jadi pas…

      sekalian, kalo mau belajar test harus mulai dari mana yaa?

      mohon pencerahan nya yaa kakak/abang sekaliaan…

      maap sebelum nya kalo bahasa ane kurang di mengrtii...
    • Ignatius Reza Lesmana
      Matcher defaultnya ngetest ke subject .. karena di subject block nya di set ke @faculty .. di should_be_valid jg ngetest ke @faculty  Btw.. before block yg
      Message 2 of 8 , Sep 28, 2013
      • 0 Attachment
        Matcher defaultnya ngetest ke "subject" .. karena di subject block nya di set ke @faculty .. di should_be_valid jg ngetest ke @faculty 

        Btw.. before block yg pertama redundant tuh... bisa dihapus aja.. langsung di subject block nya panggil FactoryGirl.create nya..



        -------- Original message --------
        From: Fauzan Qadri <ojankill@...>
        Date: 28/09/2013 7:50 PM (GMT+08:00)
        To: id-ruby@yahoogroups.com
        Subject: [id-ruby] Numpang nanya doong...


        Sore kak / bang…
        btw salam kenal yaa semua nyaa…

        ane lagi belajar2 testing pake rspec nih kak / bang…

        ane punya contoh kasus kaya gini…

        ---------------------------------------------------------------------------------------

        model/faculty.rb
        ----------------------
        class Faculty < ActiveRecord::Base
        validates :name, presence: true, uniqueness: true
        end

        spec/factories.rb
        -----------------------
        FactoryGirl.define do
        factory :faculty do 
         name "Some Faculty Name" 
         description "description for faculty name" 
         end
        end

        spec/models/faculty_spec.rb
        ---------------------------------------
        describe Faculty do
        before {@faculty = FactoryGirl.create(:faculty)}
        subject {@faculty}

        it { should be_valid }

        context "Ketika Nama Fakultas telah ada" do
        before do
        faculty_with_same_name = @...
        faculty_with_same_name.save
        end
        it { should_not be_valid }
        end
        end

        --------------------------------------------------------------------------------------

        pas jalanin dia hasilnya kek gini…


        nah yang ane mau tanyain…

        sebener nya " it { should_not be_valid } " pads context "Ketika Nama Fakultas telah ada" itu nge-test yang mana…?

        ketika di faculty_spec.rb ane ubah jadi…

        ---------------------------------------------------------------------------------------------------------------------------------
        spec/models/faculty_spec.rb
        ---------------------------------------------------------------------------------------------------------------------------------
        describe Faculty do
        before {@faculty = Faculty.new({name: "Some Name", description: "Simple description"})}
        subject {@faculty}

        it { should be_valid }

        context "Ketika Nama Fakultas telah ada" do
        before do
        faculty_with_same_name = @...
        faculty_with_same_name.save
        end
        it { should_not be_valid }
        end
        end
        ---------------------------------------------------------------------------------------------------------------------------------

        hasil test nya jadi pas…

        sekalian, kalo mau belajar test harus mulai dari mana yaa?

        mohon pencerahan nya yaa kakak/abang sekaliaan…

        maap sebelum nya kalo bahasa ane kurang di mengrtii...
      • Ahmy Yulrizka
        HI, saya coba sebisa mungkin. subject & rspec helper pertama block subject {@faculty} itu berkorespondensi dengan block it { should be_valid } artinya should
        Message 3 of 8 , Sep 28, 2013
        • 0 Attachment
          HI,

          saya coba sebisa mungkin.
          subject & rspec helper

          pertama block

          subject {@faculty}

          itu berkorespondensi dengan block

          it { should be_valid }

          artinya should be_valid akan dieksekusi dalam konteks @faculty. jadi blok
          diatas sama dengan:

          @... be_valid

          sedangkan sould be_valid adalah helper method rspec. yang akan di test
          sebenarnya adalah (lebih kurang)

          @...? === true

          contoh lain should be_nil

          @...? === true

          should_not be_valid

          @...? !== true

          Kasus 1

          pada contoh yang pertama, should_not be_valid —> @..._no
          be_validbukan faculty_with_same_name.should_not
          be_valid

          lebih jelasnya sebenarnya yang terjadi adalah kira-kira seperti ini


          ➜ spec rails cLoading development environment (Rails 4.0.0)
          irb(main):001:0> FactoryGirl.find_definitions
          => ["/private/tmp/foo/factories", "/private/tmp/foo/test/factories",
          "/private/tmp/foo/spec/factories"]
          irb(main):002:0> @faculty = FactoryGirl.create(:faculty)
          (0.1ms) begin transaction
          Faculty Exists (0.2ms) SELECT 1 AS one FROM "faculties" WHERE
          "faculties"."name" = 'Some Faculty Name' LIMIT 1
          SQL (3.7ms) INSERT INTO "faculties" ("created_at", "description",
          "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sat, 28 Sep
          2013 18:38:22 UTC +00:00], ["description", "description for faculty
          name"], ["name", "Some Faculty Name"], ["updated_at", Sat, 28 Sep 2013
          18:38:22 UTC +00:00]]
          (0.8ms) commit transaction
          => #<Faculty id: 2, name: "Some Faculty Name", description:
          "description for faculty name", created_at: "2013-09-28 18:38:22",
          updated_at: "2013-09-28 18:38:22">
          irb(main):003:0> faculty_with_same_name = @...
          => #<Faculty id: nil, name: "Some Faculty Name", description:
          "description for faculty name", created_at: nil, updated_at: nil>
          irb(main):004:0> faculty_with_same_name.save
          (0.1ms) begin transaction
          Faculty Exists (0.3ms) SELECT 1 AS one FROM "faculties" WHERE
          "faculties"."name" = 'Some Faculty Name' LIMIT 1
          (0.1ms) rollback transaction
          => false
          irb(main):006:0> @...?
          Faculty Exists (0.2ms) SELECT 1 AS one FROM "faculties" WHERE
          ("faculties"."name" = 'Some Faculty Name' AND "faculties"."id" != 2)
          LIMIT 1
          => true
          irb(main):007:0> faculty_with_same_name.valid?
          Faculty Exists (0.4ms) SELECT 1 AS one FROM "faculties" WHERE
          "faculties"."name" = 'Some Faculty Name' LIMIT 1
          => false

          jadi @faculty berhasil dibuat dengan id 1, sehingga @...? ===
          true, tetapi faculty_with_same_name.valid? === false.
          Kasus ke dua

          kira kira runtutanya seperti ini


          ➜ spec rails cLoading development environment (Rails 4.0.0)
          irb(main):001:0> @faculty = Faculty.new({name: "Some Name",
          description: "Simple description"})
          => #<Faculty id: nil, name: "Some Name", description: "Simple
          description", created_at: nil, updated_at: nil>
          irb(main):002:0> @...?
          Faculty Exists (0.2ms) SELECT 1 AS one FROM "faculties" WHERE
          "faculties"."name" = 'Some Name' LIMIT 1
          => true
          # sampai sini, @faculty belum di save, sehingga masih uniqueness valid

          irb(main):003:0> faculty_with_same_name = @...
          => #<Faculty id: nil, name: "Some Name", description: "Simple
          description", created_at: nil, updated_at: nil>
          irb(main):004:0> faculty_with_same_name.save
          (0.2ms) begin transaction
          Faculty Exists (0.2ms) SELECT 1 AS one FROM "faculties" WHERE
          "faculties"."name" = 'Some Name' LIMIT 1
          SQL (4.8ms) INSERT INTO "faculties" ("created_at", "description",
          "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sat, 28 Sep
          2013 18:48:40 UTC +00:00], ["description", "Simple description"],
          ["name", "Some Name"], ["updated_at", Sat, 28 Sep 2013 18:48:40 UTC
          +00:00]]
          (0.7ms) commit transaction
          => true
          # sampai sini fakultas dengan nama 'Some Name' terekam pada database

          irb(main):005:0> @...?
          Faculty Exists (0.3ms) SELECT 1 AS one FROM "faculties" WHERE
          "faculties"."name" = 'Some Name' LIMIT 1
          => false
          # @faculty masih memiliki id = nil, sehingga .valid? akan mengecek
          pada database dengan nama 'Some Name' dan ternyata ada, maka @faculty
          uniqueness invalid!

          irb(main):006:0> faculty_with_same_name.valid?
          Faculty Exists (0.3ms) SELECT 1 AS one FROM "faculties" WHERE
          ("faculties"."name" = 'Some Name' AND "faculties"."id" != 3) LIMIT 1
          => true
          # faculty_with_same_name yang sebelumnya terekam pada database sudah
          memiliki id. dan memang merupakan record yang valid.

          Semoga contohnya bisa membantu menjelaskan apa yang terjadi.


          Saya pribadi belajar Rspec dengan buku “The Rspec Book”. Menurut saya sih
          sangat membantu memahami metode Test Driven Development (TDD) / Behavior
          Driven Development (BDD).
          Buku tersebut karangan David Chelimsky (author rspec).

          Kalo boleh saya sarankan, untuk tetap menggunakan bahasa inggris waktu
          menulis spec. Karena Bahasa (ato Domain Specific Language/DSL) RSpec dibuat
          memang mengikuti kaidah bahasa Inggris.

          contohnya @... not_be valid. Hal ini juga akan terlihat pada
          hasil/error report rspec. pada contoh diatas : “Faculty Ketika Nama
          Fakultas telah ada should not be valid”

          mungkin lebih jelas apabila :

          context "Double faculty name" do
          ...
          it { should_not be valid }end

          struktur terlihat baik, dan error message nya pun akan menjadi lebih jelas:
          “Double faculty name should not be valid”

          Cheers.

          --
          Ahmy Yulrizka
          http://ahmy.yulrizka.com
          @yulrizka


          2013/9/28 Fauzan Qadri <ojankill@...>

          > Sore kak / bang…
          > btw salam kenal yaa semua nyaa…
          >
          > ane lagi belajar2 testing pake rspec nih kak / bang…
          >
          > ane punya contoh kasus kaya gini…
          >
          >
          > ---------------------------------------------------------------------------------------
          >
          > model/faculty.rb
          > ----------------------
          > class Faculty < ActiveRecord::Base
          > validates :name, presence: true, uniqueness: true
          > end
          >
          > spec/factories.rb
          > -----------------------
          > FactoryGirl.define do
          > factory :faculty do
          > name "Some Faculty Name"
          > description "description for faculty name"
          > end
          > end
          >
          > spec/models/faculty_spec.rb
          > ---------------------------------------
          > describe Faculty do
          > before {@faculty = FactoryGirl.create(:faculty)}
          > subject {@faculty}
          >
          > it { should be_valid }
          >
          > context "Ketika Nama Fakultas telah ada" do
          > before do
          > faculty_with_same_name = @...
          > faculty_with_same_name.save
          > end
          > it { should_not be_valid }
          > end
          > end
          >
          >
          > --------------------------------------------------------------------------------------
          >
          > pas jalanin dia hasilnya kek gini…
          >
          >
          > nah yang ane mau tanyain…
          >
          > sebener nya " it { should_not be_valid } " pads context "Ketika Nama
          > Fakultas telah ada" itu nge-test yang mana…?
          >
          > ketika di faculty_spec.rb ane ubah jadi…
          >
          >
          > ---------------------------------------------------------------------------------------------------------------------------------
          > spec/models/faculty_spec.rb
          >
          > ---------------------------------------------------------------------------------------------------------------------------------
          > describe Faculty do
          > before {@faculty = Faculty.new({name: "Some Name", description: "Simple
          > description"})}
          > subject {@faculty}
          >
          > it { should be_valid }
          >
          > context "Ketika Nama Fakultas telah ada" do
          > before do
          > faculty_with_same_name = @...
          > faculty_with_same_name.save
          > end
          > it { should_not be_valid }
          > end
          > end
          >
          > ---------------------------------------------------------------------------------------------------------------------------------
          >
          > hasil test nya jadi pas…
          >
          > sekalian, kalo mau belajar test harus mulai dari mana yaa?
          >
          > mohon pencerahan nya yaa kakak/abang sekaliaan…
          >
          > maap sebelum nya kalo bahasa ane kurang di mengrtii...
          >


          [Non-text portions of this message have been removed]
        • aviandri hidayat
          Sekilas liat sih spec yg pertama : - karena subject nya @faculty, ketika di assert it{should_not be_valid} hasilnya akan failed, karena @faculty sudah dicreate
          Message 4 of 8 , Sep 28, 2013
          • 0 Attachment
            Sekilas liat sih spec yg pertama :
            - karena subject nya @faculty, ketika di assert it{should_not be_valid} hasilnya akan failed, karena @faculty sudah dicreate terlebih dahulu ketika belom ada data di test db. 
            - Seharunya yg di assert should_not be_valid nya faculty_with_same_name

            Kind regards,
            Aviandri

            Sent from Mailbox for iPhone


            On Sat, Sep 28, 2013 at 6:50 PM, Fauzan Qadri <ojankill@...> wrote:

            Sore kak / bang…

            btw salam kenal yaa semua nyaa…

            ane lagi belajar2 testing pake rspec nih kak / bang…

            ane punya contoh kasus kaya gini…

            ---------------------------------------------------------------------------------------

            model/faculty.rb
            ----------------------
            class Faculty < ActiveRecord::Base
            validates :name, presence: true, uniqueness: true
            end

            spec/factories.rb
            -----------------------
            FactoryGirl.define do
            factory :faculty do 
             name "Some Faculty Name" 
             description "description for faculty name" 
             end
            end

            spec/models/faculty_spec.rb
            ---------------------------------------
            describe Faculty do
            before {@faculty = FactoryGirl.create(:faculty)}
            subject {@faculty}

            it { should be_valid }

            context "Ketika Nama Fakultas telah ada" do
            before do
            faculty_with_same_name = @...
            faculty_with_same_name.save
            end
            it { should_not be_valid }
            end
            end

            --------------------------------------------------------------------------------------

            pas jalanin dia hasilnya kek gini…

            <Screen Shot 2013-09-28 at 6.39.09 PM.png>

            nah yang ane mau tanyain…

            sebener nya " it { should_not be_valid } " pads context "Ketika Nama Fakultas telah ada" itu nge-test yang mana…?

            ketika di faculty_spec.rb ane ubah jadi…

            ---------------------------------------------------------------------------------------------------------------------------------
            spec/models/faculty_spec.rb
            ---------------------------------------------------------------------------------------------------------------------------------
            describe Faculty do
            before {@faculty = Faculty.new({name: "Some Name", description: "Simple description"})}
            subject {@faculty}

            it { should be_valid }

            context "Ketika Nama Fakultas telah ada" do
            before do
            faculty_with_same_name = @...
            faculty_with_same_name.save
            end
            it { should_not be_valid }
            end
            end
            ---------------------------------------------------------------------------------------------------------------------------------

            hasil test nya jadi pas…

            sekalian, kalo mau belajar test harus mulai dari mana yaa?

            mohon pencerahan nya yaa kakak/abang sekaliaan…

            maap sebelum nya kalo bahasa ane kurang di mengrtii...

          • Fauzan Qadri
            okee deeh... makasih yaa kak, ignatius reza lesmana, aviandri hidayat, sama ahmy yulrizka atas penjelasan nyaa� ... okee deeh... makasih yaa kak, ignatius reza
            Message 5 of 8 , Sep 28, 2013
            • 0 Attachment
              okee deeh...
              makasih yaa kak, ignatius reza lesmana, aviandri hidayat, sama ahmy yulrizka atas penjelasan nyaa…


              On Sep 28, 2013, at 9:27 PM, Ignatius Reza Lesmana <lyoneil_de_sire@...> wrote:

               

              Matcher defaultnya ngetest ke "subject" .. karena di subject block nya di set ke @faculty .. di should_be_valid jg ngetest ke @faculty 


              Btw.. before block yg pertama redundant tuh... bisa dihapus aja.. langsung di subject block nya panggil FactoryGirl.create nya..



              -------- Original message --------
              From: Fauzan Qadri <ojankill@...>
              Date: 28/09/2013 7:50 PM (GMT+08:00)
              To: id-ruby@yahoogroups.com
              Subject: [id-ruby] Numpang nanya doong...


              Sore kak / bang…
              btw salam kenal yaa semua nyaa…

              ane lagi belajar2 testing pake rspec nih kak / bang…

              ane punya contoh kasus kaya gini…

              ---------------------------------------------------------------------------------------

              model/faculty.rb
              ----------------------
              class Faculty < ActiveRecord::Base
              validates :name, presence: true, uniqueness: true
              end

              spec/factories.rb
              -----------------------
              FactoryGirl.define do
              factory :faculty do 
               name "Some Faculty Name" 
               description "description for faculty name" 
               end
              end

              spec/models/faculty_spec.rb
              ---------------------------------------
              describe Faculty do
              before {@faculty = FactoryGirl.create(:faculty)}
              subject {@faculty}

              it { should be_valid }

              context "Ketika Nama Fakultas telah ada" do
              before do
              faculty_with_same_name = @...
              faculty_with_same_name.save
              end
              it { should_not be_valid }
              end
              end

              --------------------------------------------------------------------------------------

              pas jalanin dia hasilnya kek gini…


              nah yang ane mau tanyain…

              sebener nya " it { should_not be_valid } " pads context "Ketika Nama Fakultas telah ada" itu nge-test yang mana…?

              ketika di faculty_spec.rb ane ubah jadi…

              ---------------------------------------------------------------------------------------------------------------------------------
              spec/models/faculty_spec.rb
              ---------------------------------------------------------------------------------------------------------------------------------
              describe Faculty do
              before {@faculty = Faculty.new({name: "Some Name", description: "Simple description"})}
              subject {@faculty}

              it { should be_valid }

              context "Ketika Nama Fakultas telah ada" do
              before do
              faculty_with_same_name = @...
              faculty_with_same_name.save
              end
              it { should_not be_valid }
              end
              end
              ---------------------------------------------------------------------------------------------------------------------------------

              hasil test nya jadi pas…

              sekalian, kalo mau belajar test harus mulai dari mana yaa?

              mohon pencerahan nya yaa kakak/abang sekaliaan…

              maap sebelum nya kalo bahasa ane kurang di mengrtii...


            • Ronny Haryanto
              Halo, Saran saya kalo masih belajar, mendingan jangan diribetin dengan FactoryGirl, Machinist, dsb dulu. Kalo saya yg nulis tesnya, kira2 gini: class Faculty
              Message 6 of 8 , Sep 29, 2013
              • 0 Attachment
                Halo,

                Saran saya kalo masih belajar, mendingan jangan diribetin dengan
                FactoryGirl, Machinist, dsb dulu. Kalo saya yg nulis tesnya, kira2 gini:

                class Faculty < ActiveRecord::Base
                validates :name, presence: true, uniqueness: true
                end

                describe Faculty do
                subject(:faculty) { Faculty.new(name: name, description: description) }
                let(:name) { "Fakultas Pengetesan" }
                let(:description) { "Kerjanya ngetes mulu" }

                specify { expect(faculty).to be_valid }

                context "when a faculty with the same name already exists" do
                before { Faculty.create!(name: name) }
                specify { expect(faculty).to_not be_valid }
                end

                context "when a faculty name is blank" do
                let(:name) { "" }
                specify { expect(faculty).to_not be_valid }
                end
                end


                Subject itu adalah yg subjek yg sedang dites. Saya lebih suka memberi nama
                subjectnya dan mengacunya pake nama (e.g. `faculty`) ketimbang pake
                `subject`, karena lebih jelas.

                `specify` ini format baru yg disarankan skrg dari `it { should ... }`
                (deprecated). Ini versi lebih singkat aja dibanding nulis:

                it "should not be valid" do
                expect(faculty).to_not be_valid
                end

                Si RSpec bisa generate sendiri label "should not be valid" itu dari specnya
                (makanya namanya specify).

                `expect(...).to ...` ini juga versi baru dari `blah.should ...` yg bakal
                deprecated soon.

                `faculty` tidak ada yg perlu disave untuk bisa dites, kecuali ngetes
                uniqueness. Ini kunci kenapa tes anda failing. Karena subjek anda dibikin
                pake FactoryGirl.create yg akan bikin AR object dan ngesave di DB.
                Sedangkan Faculty.new (atau FactoryGirl.build) akan bikin AR objectnya
                doang tanpa ngesave di DB.

                Poin penting lainnya, subject dan let itu lazily evaluated, jadi pas
                dipanggil baru dijalanin blocknya, sedangkan before block jalan duluan
                sebelum spec/it. Di tes anda, before block yg paling pertama itu dijalankan
                duluan, jadi @faculty udah disave ke DB duluan. Lalu baru before block yg
                di dalem context dijalanin, nah ini urutannya kan salah setupnya.
                Seharusnya yg kita mau adalah before block di dalem context ini dijalanin
                duluan, baru subjeknya dibikin. Makanya lebih baik subject itu lazily
                evaluated (bikin objectnya di dalam blocknya subject, seperti contoh saya),
                jangan dibikin di dalam before block.

                Ronny

                2013/9/28 Fauzan Qadri <ojankill@...>

                > Sore kak / bang…
                > btw salam kenal yaa semua nyaa…
                >
                > ane lagi belajar2 testing pake rspec nih kak / bang…
                >
                > ane punya contoh kasus kaya gini…
                >
                >
                > ---------------------------------------------------------------------------------------
                >
                > model/faculty.rb
                > ----------------------
                > class Faculty < ActiveRecord::Base
                > validates :name, presence: true, uniqueness: true
                > end
                >
                > spec/factories.rb
                > -----------------------
                > FactoryGirl.define do
                > factory :faculty do
                > name "Some Faculty Name"
                > description "description for faculty name"
                > end
                > end
                >
                > spec/models/faculty_spec.rb
                > ---------------------------------------
                > describe Faculty do
                > before {@faculty = FactoryGirl.create(:faculty)}
                > subject {@faculty}
                >
                > it { should be_valid }
                >
                > context "Ketika Nama Fakultas telah ada" do
                > before do
                > faculty_with_same_name = @...
                > faculty_with_same_name.save
                > end
                > it { should_not be_valid }
                > end
                > end
                >
                >
                > --------------------------------------------------------------------------------------
                >
                > pas jalanin dia hasilnya kek gini…
                >
                >
                > nah yang ane mau tanyain…
                >
                > sebener nya " it { should_not be_valid } " pads context "Ketika Nama
                > Fakultas telah ada" itu nge-test yang mana…?
                >
                > ketika di faculty_spec.rb ane ubah jadi…
                >
                >
                > ---------------------------------------------------------------------------------------------------------------------------------
                > spec/models/faculty_spec.rb
                >
                > ---------------------------------------------------------------------------------------------------------------------------------
                > describe Faculty do
                > before {@faculty = Faculty.new({name: "Some Name", description: "Simple
                > description"})}
                > subject {@faculty}
                >
                > it { should be_valid }
                >
                > context "Ketika Nama Fakultas telah ada" do
                > before do
                > faculty_with_same_name = @...
                > faculty_with_same_name.save
                > end
                > it { should_not be_valid }
                > end
                > end
                >
                > ---------------------------------------------------------------------------------------------------------------------------------
                >
                > hasil test nya jadi pas…
                >
                > sekalian, kalo mau belajar test harus mulai dari mana yaa?
                >
                > mohon pencerahan nya yaa kakak/abang sekaliaan…
                >
                > maap sebelum nya kalo bahasa ane kurang di mengrtii...
                >


                [Non-text portions of this message have been removed]
              • Ronny Haryanto
                ... Walaupun hasilnya sama, spesifikasinya akan jadi agak aneh ketika dibaca kalo kayak gitu, karena yg dites bukan lagi subjeknya. Akan lebih tidak
                Message 7 of 8 , Sep 29, 2013
                • 0 Attachment
                  On Sun, Sep 29, 2013 at 5:15 AM, aviandri hidayat <aviandri@...> wrote:
                  - Seharunya yg di assert should_not be_valid nya faculty_with_same_name

                  Walaupun hasilnya sama, spesifikasinya akan jadi agak aneh ketika dibaca kalo kayak gitu, karena yg dites bukan lagi subjeknya. Akan lebih tidak membingungkan kalo "it" selalu mengacu ke `subject`.

                  Ronny
                • Fauzan Qadri
                  okeyy di mengerti bang ronnyà :) mantaabà ... okeyy di mengerti bang ronnyà :) mantaabà On Sep 30, 2013, at 8:01 AM, Ronny Haryanto
                  Message 8 of 8 , Sep 30, 2013
                  • 0 Attachment
                    okeyy
                    di mengerti bang ronny… :)
                    mantaab…

                    On Sep 30, 2013, at 8:01 AM, Ronny Haryanto <ronny@...> wrote:

                     

                    Halo,

                    Saran saya kalo masih belajar, mendingan jangan diribetin dengan
                    FactoryGirl, Machinist, dsb dulu. Kalo saya yg nulis tesnya, kira2 gini:

                    class Faculty < ActiveRecord::Base
                    validates :name, presence: true, uniqueness: true
                    end

                    describe Faculty do
                    subject(:faculty) { Faculty.new(name: name, description: description) }
                    let(:name) { "Fakultas Pengetesan" }
                    let(:description) { "Kerjanya ngetes mulu" }

                    specify { expect(faculty).to be_valid }

                    context "when a faculty with the same name already exists" do
                    before { Faculty.create!(name: name) }
                    specify { expect(faculty).to_not be_valid }
                    end

                    context "when a faculty name is blank" do
                    let(:name) { "" }
                    specify { expect(faculty).to_not be_valid }
                    end
                    end

                    Subject itu adalah yg subjek yg sedang dites. Saya lebih suka memberi nama
                    subjectnya dan mengacunya pake nama (e.g. `faculty`) ketimbang pake
                    `subject`, karena lebih jelas.

                    `specify` ini format baru yg disarankan skrg dari `it { should ... }`
                    (deprecated). Ini versi lebih singkat aja dibanding nulis:

                    it "should not be valid" do
                    expect(faculty).to_not be_valid
                    end

                    Si RSpec bisa generate sendiri label "should not be valid" itu dari specnya
                    (makanya namanya specify).

                    `expect(...).to ...` ini juga versi baru dari `blah.should ...` yg bakal
                    deprecated soon.

                    `faculty` tidak ada yg perlu disave untuk bisa dites, kecuali ngetes
                    uniqueness. Ini kunci kenapa tes anda failing. Karena subjek anda dibikin
                    pake FactoryGirl.create yg akan bikin AR object dan ngesave di DB.
                    Sedangkan Faculty.new (atau FactoryGirl.build) akan bikin AR objectnya
                    doang tanpa ngesave di DB.

                    Poin penting lainnya, subject dan let itu lazily evaluated, jadi pas
                    dipanggil baru dijalanin blocknya, sedangkan before block jalan duluan
                    sebelum spec/it. Di tes anda, before block yg paling pertama itu dijalankan
                    duluan, jadi @faculty udah disave ke DB duluan. Lalu baru before block yg
                    di dalem context dijalanin, nah ini urutannya kan salah setupnya.
                    Seharusnya yg kita mau adalah before block di dalem context ini dijalanin
                    duluan, baru subjeknya dibikin. Makanya lebih baik subject itu lazily
                    evaluated (bikin objectnya di dalam blocknya subject, seperti contoh saya),
                    jangan dibikin di dalam before block.

                    Ronny

                    2013/9/28 Fauzan Qadri <ojankill@...>

                    > Sore kak / bang…
                    > btw salam kenal yaa semua nyaa…
                    >
                    > ane lagi belajar2 testing pake rspec nih kak / bang…
                    >
                    > ane punya contoh kasus kaya gini…
                    >
                    >
                    > ----------------------------------------------------------
                    >
                    > model/faculty.rb
                    > ----------------------
                    > class Faculty < ActiveRecord::Base
                    > validates :name, presence: true, uniqueness: true
                    > end
                    >
                    > spec/factories.rb
                    > -----------------------
                    > FactoryGirl.define do
                    > factory :faculty do
                    > name "Some Faculty Name"
                    > description "description for faculty name"
                    > end
                    > end
                    >
                    > spec/models/faculty_spec.rb
                    > ---------------------------------------
                    > describe Faculty do
                    > before {@faculty = FactoryGirl.create(:faculty)}
                    > subject {@faculty}
                    >
                    > it { should be_valid }
                    >
                    > context "Ketika Nama Fakultas telah ada" do
                    > before do
                    > faculty_with_same_name = @...
                    > faculty_with_same_name.save
                    > end
                    > it { should_not be_valid }
                    > end
                    > end
                    >
                    >
                    > ----------------------------------------------------------
                    >
                    > pas jalanin dia hasilnya kek gini…
                    >
                    >
                    > nah yang ane mau tanyain…
                    >
                    > sebener nya " it { should_not be_valid } " pads context "Ketika Nama
                    > Fakultas telah ada" itu nge-test yang mana…?
                    >
                    > ketika di faculty_spec.rb ane ubah jadi…
                    >
                    >
                    > ----------------------------------------------------------
                    > spec/models/faculty_spec.rb
                    >
                    > ----------------------------------------------------------
                    > describe Faculty do
                    > before {@faculty = Faculty.new({name: "Some Name", description: "Simple
                    > description"})}
                    > subject {@faculty}
                    >
                    > it { should be_valid }
                    >
                    > context "Ketika Nama Fakultas telah ada" do
                    > before do
                    > faculty_with_same_name = @...
                    > faculty_with_same_name.save
                    > end
                    > it { should_not be_valid }
                    > end
                    > end
                    >
                    > ----------------------------------------------------------
                    >
                    > hasil test nya jadi pas…
                    >
                    > sekalian, kalo mau belajar test harus mulai dari mana yaa?
                    >
                    > mohon pencerahan nya yaa kakak/abang sekaliaan…
                    >
                    > maap sebelum nya kalo bahasa ane kurang di mengrtii...
                    >

                    [Non-text portions of this message have been removed]


                  Your message has been successfully submitted and would be delivered to recipients shortly.