Skip to content

Instantly share code, notes, and snippets.

class ShowRateUsLogicTest : Spek({
// property declaration, setup and preparation are skipped
describe("show rate us logic") {
context("first conditions checks") {
context("buy clicked once") {
beforeEachTest {
prepareConditions(buyClickedTimes = 1)
}
it("should not show 'rate us'") {
class ShowRateUsLogicTest {
// property declaration and setup are skipped
private fun prepareConditions(
buyClickedTimes: Int = 0,
isNeverShownAgainClicked: Boolean = false,
isRateNowClicked: Boolean = false,
lastShownTimeMillis: Long = 0,
currentTimeMillis: Long = 0
) {
repeat(buyClickedTimes) { buyPreferences.incrementBuyCount() }
public class ShowRateUsLogicTest {
// property declaration is skipped
@Before public void setUp() {
final Context mockedContext = new SPMockBuilder().createContext();
rateUsPreferences = new RateUsPreferencesImpl(mockedContext);
buyPreferences = new BuyPreferencesImpl(mockedContext);
time = Mockito.mock(Time.class);
showRateUsLogic = new ShowRateUsLogic(rateUsPreferences, buyPreferences, time);
}
public class ShowRateUsLogicTest {
// property declaration is skipped
@Before public void setUp() {
rateUsPreferences = new RateUsPreferencesMock();
buyPreferences = new BuyPreferencesMock();
time = new TimeMock();
showRateUsLogic = new ShowRateUsLogic(rateUsPreferences, buyPreferences, time);
}
@Test public void onFirstCheckAndOneClickItShouldNotShow() {
public class ShowRateUsLogicTest {
private RateUsPreferencesMock rateUsPreferences;
private BuyPreferencesMock buyPreferences;
private TimeMock time;
private ShowRateUsLogic showRateUsLogic;
@Test public void test1() {
rateUsPreferences = new RateUsPreferencesMock();
buyPreferences = new BuyPreferencesMock();
time = new TimeMock();
public class BuyPreferencesMock implements BuyPreferences {
private int count;
@Override public void incrementBuyCount() {
++count;
}
@Override public int getBuyCount() {
return count;
}
class ShowRateUsLogic(
private val rateUsPreferences: RateUsPreferences,
private val buyPreferences: BuyPreferences,
private val time: Time
) {
fun shouldShowRateUs(): Boolean {
val timeFromLastShown = time.getCurrentTimeMillis() - rateUsPreferences.getLastShownTimeMillis()
return when {
// User doesn't want to see "rate us" again
rateUsPreferences.isNeverShownAgainClicked() -> false
interface Time {
fun getCurrentTimeMillis(): Long
}
class TimeImpl : Time {
override fun getCurrentTimeMillis() = System.currentTimeMillis()
}
class BuyPreferencesImpl(context: Context) : BuyPreferences {
companion object {
private val PREFERENCES_FILENAME = "BUY_PREFERENCES"
private val BUY_COUNT_KEY = "BUY_COUNT_KEY"
}
private val sharedPreferences = context.getSharedPreferences(
PREFERENCES_FILENAME,
Context.MODE_PRIVATE
)
interface BuyPreferences {
fun incrementBuyCount()
fun getBuyCount(): Int
}
class BuyPreferencesImpl(context: Context) : BuyPreferences {
// ...
private val sharedPreferences: SharedPreferences = context.getSharedPreferences(...)
override fun incrementBuyCount() {