Saving Data on Android


com.raywenderlich.sqlitetodo (test)



Download 19,28 Mb.
Pdf ko'rish
bet72/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   68   69   70   71   72   73   74   75   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

com.raywenderlich.sqlitetodo (test)
 and select 
New
 > 
Kotlin File/Class
. Name the 
class 
ToDoDatabaseTest
 and click 
OK
. Open the file and add the code:
@RunWith(RobolectricTestRunner::class)
 
class
 
TestDatabase
 { 
  
// 1
 
  
lateinit
 
var
 dbHelper: ToDoDatabaseHandler 
 
  
// 2
 
  
@Before
 
  
fun
 
setup
()
 { 
    dbHelper = 
ToDoDatabaseHandler(RuntimeEnvironment.application) 
    dbHelper.clearDbAndRecreate() 
  } 
 
 
  
@Test
 
  
@Throws(Exception::class)
 
  
fun
 
testDbInsertion
()
 { 
    
// 3
 
    
// Given
 
    
val
 item1 = ToDo(
0

"Test my Program"

false

    
val
 item2 = ToDo(
0

"Test my Program Again"

false

 
    
// 4
 
    
// When
 
    dbHelper.createToDo(item1) 
    dbHelper.createToDo(item2) 
 
    
// 5
 
    
// Then
 
    
val
 allText = dbHelper.getAllText() 
    
val
 expectedData = 
"
${item1.toDoName}${item2.toDoName}
"
 
    Assert.assertEquals(allText, expectedData) 
  } 
   
  
// 6
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
66


  
@After
 
  
fun
 
tearDown
()
 { 
    dbHelper.clearDb() 
  } 
}
With the above, you:
1. First, declare a database helper property, which you will test.
2. Perform setup by initializing the database, then clear and recreate the database 
to start with a fresh copy every time. The function annotated with 
@Setup
 will be 
the first function to run before every test.
3. The test is written in 
cucumber style
 or 
Given/When/Then
. You first create two 
TODO items with two different names. Given those items...
4. ...when they are added to the database...
5. ...then Assert that 
getAllText
 matches the expected value.
6. Clear the database after the test, just to be sure.
To run the test, click the green arrow next to the class declaration and select 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   68   69   70   71   72   73   74   75   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish