Coder Social home page Coder Social logo

robospecs's Introduction

RoboSpecs

RoboSpecs lets you use Robolectric with Specs2.

Using Robolectric with Specs2 means that you mean write unit tests for your Android apps in Scala and run them in a JVM. Without Robolectric, Android unit tests must be run in an emulator. This is slow and painful enough to make TDD impossible with Android.

Robolectric provides a JUnit test runner which does some class loader magic at runtime to give your unit tests fake versions of Android framework classes. Robospecs is similar to Robolectric's JUnit test runner. Robospecs does not use JUnit.

Using RoboSpecs is really easy.

Download the latest jar

Get it here GitHub

Add an SBT dependency

val robospecs = "com.github.jbrechtel" %% "robospecs" % "0.2" % "test"
val robospecsSnapshots  = "snapshots" at "http://jbrechtel.github.com/repo/snapshots"
val snapshots = "snapshots" at "http://scala-tools.org/repo-snapshots"
val releases  = "releases" at "http://scala-tools.org/repo-releases"

Add a Maven dependency

<dependency>
    <groupId>com.github.jbrechtel</groupId>
    <artifactId>robospecs</artifactId>
    <version>0.2</version>
    <scope>test</scope>
</dependency>

Note: RoboSpecs is not thread safe

RoboSpecs' intialization of Robolectric (and possibly Robolectric itself) is not thread safe. By default SBT executes test suites in parallel and Specs2 runs specs within a suite in parallel. Add the following SBT options to run tests in serial.

parallelExecution in Test := false,
testOptions in Test += Tests.Argument("sequential"),

WARNING: When you use RoboSpecs do not attempt to place Android SDK code within the "should {} codeblock" or outside of it. Robolectric shadows are only attached within the "in {} codeblocks", so Android SDK code only works within them.

Example project

See a very simple, but working, example project here: https://github.com/jbrechtel/robospecs_example

Here's the specs from that projects

Then you can mix it into your Specs2 specs like so:

package com.brechtel.toaster

import org.specs2.mutable._
import org.specs2.mock.Mockito
import org.specs2.matcher.{Expectable, Matcher}
import org.specs2.specification.BeforeExample
import com.github.jbrechtel.robospecs.RoboSpecs
import com.xtremelabs.robolectric.shadows._

class MainActivitySpecs extends RoboSpecs with Mockito {

  "onCreate" should {
    "set the content view" in {
      val activity = new MainActivity()
      activity.onCreate(null)
      activity.findViewById(R.id.message) must not beNull
    }
  }

  "showMessageButton" should {
    "be the Show Message button in the view" in {
      val activity = new MainActivity()
      activity.onCreate(null)
      activity.showMessageButton == activity.findViewById(R.id.show_message)
    }
  }

  "messageEditText" should {
    "be the EditText in the view" in {
      val activity = new MainActivity()
      activity.onCreate(null)
      activity.messageEditText == activity.findViewById(R.id.message)
    }
  }

  "clicking the showMessageButton" should {
    "show a toast popup with text from the message input field" in {
      val activity = new MainActivity()
      activity.onCreate(null)
      activity.messageEditText.setText("expected message")
      activity.showMessageButton.performClick()
      ShadowToast.getTextOfLatestToast must beEqualTo("expected message")
    }
  }
}

robospecs's People

Contributors

cessationoftime avatar etorreborre avatar jbrechtel avatar sdb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

robospecs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.