func Test_Mock_AssertNumberOfCalls(t *testing.T) { var mockedService *TestExampleImplementation = new(TestExampleImplementation) mockedService.On("Test_Mock_AssertNumberOfCalls", 1, 2, 3).Return(5, 6, 7) mockedService.Called(1, 2, 3) assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertNumberOfCalls", 1)) mockedService.Called(1, 2, 3) assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertNumberOfCalls", 2)) }
func TestSimpleExample(t *testing.T) { // build a map from a JSON object o := MustFromJSON(`{"name":"Mat","foods":["indian","chinese"], "location":{"county":"hobbiton","city":"the shire"}}`) // Map can be used as a straight map[string]interface{} assert.Equal(t, o["name"], "Mat") // Get an Value object v := o.Get("name") assert.Equal(t, v, &Value{data: "Mat"}) // Test the contained value assert.False(t, v.IsInt()) assert.False(t, v.IsBool()) assert.True(t, v.IsStr()) // Get the contained value assert.Equal(t, v.Str(), "Mat") // Get a default value if the contained value is not of the expected type or does not exist assert.Equal(t, 1, v.Int(1)) // Get a value by using array notation assert.Equal(t, "indian", o.Get("foods[0]").Data()) // Set a value by using array notation o.Set("foods[0]", "italian") assert.Equal(t, "italian", o.Get("foods[0]").Str()) // Get a value by using dot notation assert.Equal(t, "hobbiton", o.Get("location.county").Str()) }
// AssertCalled asserts that the method was called. func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool { if !assert.True(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method should have been called with %d argument(s), but was not.", methodName, len(arguments))) { t.Logf("%s", m.ExpectedCalls) return false } return true }
func TestScheduleShouldStartAndStop(t *testing.T) { defer glog.Flush() assert := assert.New(t) unit := 10 * time.Millisecond calledCount := 0 testF := func(start time.Time, dur time.Time) { remainder := start.Nanosecond() % int(unit) assert.Equal(0, remainder, "Start not truncated to 10ms") calledCount++ } s := schedule.Schedule{ Callback: testF, Period: unit, } assert.NotNil(s) sleepCount := 3 start := time.Now() err := s.Start() assert.Nil(err) time.Sleep(unit * time.Duration(sleepCount)) s.Stop() elapsed := time.Since(start) assert.True(calledCount >= sleepCount && calledCount <= int(elapsed/unit)) }
func Test_Arguments_Assert(t *testing.T) { var args Arguments = []interface{}{"string", 123, true} assert.True(t, args.Assert(t, "string", 123, true)) }
func Test_Mock_AssertCalled_WithArguments_With_Repeatability(t *testing.T) { var mockedService *TestExampleImplementation = new(TestExampleImplementation) mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Once() mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4).Return(5, 6, 7).Once() mockedService.Called(1, 2, 3) mockedService.Called(2, 3, 4) tt := new(testing.T) assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3)) assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4)) assert.False(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 3, 4, 5)) }
func Test_Arguments_Is(t *testing.T) { var args Arguments = []interface{}{"string", 123, true} assert.True(t, args.Is("string", 123, true)) assert.False(t, args.Is("wrong", 456, false)) }
func TestHas(t *testing.T) { m := New(TestMap) assert.True(t, m.Has("name")) assert.True(t, m.Has("address.state")) assert.True(t, m.Has("numbers[4]")) assert.False(t, m.Has("address.state.nope")) assert.False(t, m.Has("address.nope")) assert.False(t, m.Has("nope")) assert.False(t, m.Has("numbers[5]")) m = nil assert.False(t, m.Has("nothing")) }
func Test_Mock_AssertCalled_WithAnythingOfTypeArgument(t *testing.T) { var mockedService *TestExampleImplementation = new(TestExampleImplementation) mockedService.On("Test_Mock_AssertCalled_WithAnythingOfTypeArgument", Anything, Anything, Anything).Return() mockedService.Called(1, "two", []uint8("three")) assert.True(t, mockedService.AssertCalled(t, "Test_Mock_AssertCalled_WithAnythingOfTypeArgument", AnythingOfType("int"), AnythingOfType("string"), AnythingOfType("[]uint8"))) }
func TestDynamicConfiguratonDefaultShouldIgnoreNilConf(t *testing.T) { assert := assert.New(t) dc := config.DynamicConfiguration{ Type: "test", Conf: nil, } dc.Default() //No panics means pass assert.True(true) }
func Test_Mock_AssertCalled_WithArguments(t *testing.T) { var mockedService *TestExampleImplementation = new(TestExampleImplementation) mockedService.On("Test_Mock_AssertCalled_WithArguments", 1, 2, 3).Return(5, 6, 7) mockedService.Called(1, 2, 3) tt := new(testing.T) assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments", 1, 2, 3)) assert.False(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments", 2, 3, 4)) }
func TestHasDefaultShouldWorkWithoutPtr(t *testing.T) { assert := assert.New(t) type S struct { A int `default:"1"` } s := S{} assert.Equal(s.A, 0) def, err := HasDefault(s, "A") assert.Nil(err) assert.True(def) }
func Test_Mock_AssertExpectationsCustomType(t *testing.T) { var mockedService *TestExampleImplementation = new(TestExampleImplementation) mockedService.On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).Return(nil).Once() tt := new(testing.T) assert.False(t, mockedService.AssertExpectations(tt)) // make the call now mockedService.TheExampleMethod3(&ExampleType{}) // now assert expectations assert.True(t, mockedService.AssertExpectations(tt)) }
func Test_Mock_AssertExpectations(t *testing.T) { var mockedService *TestExampleImplementation = new(TestExampleImplementation) mockedService.On("Test_Mock_AssertExpectations", 1, 2, 3).Return(5, 6, 7) tt := new(testing.T) assert.False(t, mockedService.AssertExpectations(tt)) // make the call now mockedService.Called(1, 2, 3) // now assert expectations assert.True(t, mockedService.AssertExpectations(tt)) }
func Test_AssertExpectationsForObjects_Helper(t *testing.T) { var mockedService1 *TestExampleImplementation = new(TestExampleImplementation) var mockedService2 *TestExampleImplementation = new(TestExampleImplementation) var mockedService3 *TestExampleImplementation = new(TestExampleImplementation) mockedService1.On("Test_AssertExpectationsForObjects_Helper", 1).Return() mockedService2.On("Test_AssertExpectationsForObjects_Helper", 2).Return() mockedService3.On("Test_AssertExpectationsForObjects_Helper", 3).Return() mockedService1.Called(1) mockedService2.Called(2) mockedService3.Called(3) assert.True(t, AssertExpectationsForObjects(t, mockedService1.Mock, mockedService2.Mock, mockedService3.Mock)) }