ผลต่างระหว่างรุ่นของ "Sw-spec/unit-test-and-mock"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(หน้าที่ถูกสร้างด้วย '== CounterView.kt == <pre> interface CounterView { fun setCounter(value: Int) } </pre> == CounterPresenter.kt == <pre> class Counte...')
 
 
(ไม่แสดง 2 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 1: แถว 1:
== CounterView.kt ==
+
== Links ==
 +
 
 +
* MVP documents: TBA
 +
* [http://site.mockito.org/ Mockito]
 +
 
 +
== Codes (Counter) ==
 +
=== CounterView.kt ===
 
<pre>
 
<pre>
 
interface CounterView {
 
interface CounterView {
แถว 5: แถว 11:
 
}
 
}
 
</pre>
 
</pre>
== CounterPresenter.kt ==
+
 
 +
=== CounterPresenter.kt ===
 
<pre>
 
<pre>
 
class CounterPresenter(val view: CounterView) {
 
class CounterPresenter(val view: CounterView) {
แถว 32: แถว 39:
 
     private fun updateCounterInView() {
 
     private fun updateCounterInView() {
 
         view.setCounter(counter)
 
         view.setCounter(counter)
 +
    }
 +
}
 +
</pre>
 +
 +
=== CounterPresenterTest.kt ===
 +
<pre>
 +
import org.junit.Before
 +
import org.junit.Test
 +
import org.mockito.Mockito.*
 +
 +
/**
 +
* Created by jittat on 23/3/2561.
 +
*/
 +
class CounterPresenterTest {
 +
    lateinit var view: CounterView
 +
    lateinit var presenter: CounterPresenter
 +
 +
    @Before
 +
    fun init() {
 +
        view = mock(CounterView::class.java)
 +
        presenter = CounterPresenter(view)
 +
    }
 +
 +
    @Test
 +
    fun testResetCounter() {
 +
        presenter.start()
 +
        verify(view).setCounter(0)
 +
    }
 +
 +
    @Test
 +
    fun testAddCounter() {
 +
        presenter.start()
 +
        presenter.onPlusButtonClicked()
 +
 +
        val inOrder = inOrder(view)
 +
        inOrder.verify(view).setCounter(0)
 +
        inOrder.verify(view).setCounter(1)
 +
    }
 +
}
 +
</pre>
 +
 +
== Codes (Calculator) ==
 +
=== CalculatorView.kt ===
 +
<pre>
 +
interface CalculatorView {
 +
    fun setDisplayValue(msg: String)
 +
}
 +
</pre>
 +
 +
=== CalculatorPresenter.kt ===
 +
<pre>
 +
class CalculatorPresenter(val view: CalculatorView) {
 +
    fun start() {
 +
    }
 +
    fun onDigitClicked(digit: Int) {
 +
    }
 +
    fun onClearClicked() {
 +
    }
 +
}
 +
</pre>
 +
 +
=== CalculatorPresenterTest.kt ===
 +
<pre>
 +
import org.junit.Before
 +
import org.junit.Test
 +
import org.mockito.Mockito.mock
 +
import org.mockito.Mockito.verify
 +
 +
class CalculatorPresenterTest {
 +
    lateinit var view: CalculatorView
 +
    lateinit var presenter: CalculatorPresenter
 +
 +
    @Before
 +
    fun init() {
 +
        view = mock(CalculatorView::class.java)
 +
        presenter = CalculatorPresenter(view)
 +
    }
 +
 +
    @Test
 +
    fun testStart() {
 +
        presenter.start()
 +
        verify(view).setDisplayValue("0")
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>

รุ่นแก้ไขปัจจุบันเมื่อ 04:59, 23 มีนาคม 2561

Links

Codes (Counter)

CounterView.kt

interface CounterView {
    fun setCounter(value: Int)
}

CounterPresenter.kt

class CounterPresenter(val view: CounterView) {
    var counter: Int = 0

    fun onPlusButtonClicked() {
        counter++
        updateCounterInView()
    }
    fun onMinusButtonClicked() {
        counter--
        updateCounterInView()
    }
    fun onResetButtonClicked() {
        reset()
    }
    fun start() {
        reset()
    }

    private fun reset() {
        counter = 0
        updateCounterInView()
    }

    private fun updateCounterInView() {
        view.setCounter(counter)
    }
}

CounterPresenterTest.kt

import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.*

/**
 * Created by jittat on 23/3/2561.
 */
class CounterPresenterTest {
    lateinit var view: CounterView
    lateinit var presenter: CounterPresenter

    @Before
    fun init() {
        view = mock(CounterView::class.java)
        presenter = CounterPresenter(view)
    }

    @Test
    fun testResetCounter() {
        presenter.start()
        verify(view).setCounter(0)
    }

    @Test
    fun testAddCounter() {
        presenter.start()
        presenter.onPlusButtonClicked()

        val inOrder = inOrder(view)
        inOrder.verify(view).setCounter(0)
        inOrder.verify(view).setCounter(1)
    }
}

Codes (Calculator)

CalculatorView.kt

interface CalculatorView {
    fun setDisplayValue(msg: String)
}

CalculatorPresenter.kt

class CalculatorPresenter(val view: CalculatorView) {
    fun start() {
    }
    fun onDigitClicked(digit: Int) {
    }
    fun onClearClicked() {
    }
}

CalculatorPresenterTest.kt

import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify

class CalculatorPresenterTest {
    lateinit var view: CalculatorView
    lateinit var presenter: CalculatorPresenter

    @Before
    fun init() {
        view = mock(CalculatorView::class.java)
        presenter = CalculatorPresenter(view)
    }

    @Test
    fun testStart() {
        presenter.start()
        verify(view).setDisplayValue("0")
    }
}