Group Group Group Group Group Group Group Group Group

Observer Pattern Unit testing

I have implemented an Observer pattern to observe the changes in the view controller.
So it has one list of objects when I am updating the list count should increase by one it is increasing in the actual code but when i am unit testing test case is failing i am not able to understand why.
I have try with expectations but it is always failing.

class ConversationViewModel: ConversationViewModelProtocol {

var messages: Observable<[Message]> = Observable([])

var follower: FollowerListItemViewModel

var userName: String = “”

private var conversationRepository: ConversationRepositoryProtocol

init(userName: String, follower: FollowerListItemViewModel, conversationRepository: ConversationRepositoryProtocol) {

self.follower = follower

self.userName = userName

self.conversationRepository = conversationRepository



func bindObserver() {

conversationRepository.messages.observe(on: self) { messages in

self.updateMessageObserver(message: messages)



func sendMessage(messagebody: String) {

let message = Message(message: messagebody, messageTo: follower.followerName,

messageFrom: userName, direction: .outgoing)

updateMessageObserver(message: [message])

conversationRepository.sendMessage(message: message) { _ in

// update check box when message send complition response we received



private func updateMessageObserver(message: [Message]) {

messages.value.append(contentsOf: message)

print(“Message Count (messages.value.count)”)



func test_ReceiveMessage() throws {
let messageString = “hai”
let follwerName = “Jain”
let imagePath = “imagePath”

	let followerListViewModel = FollowerListItemViewModel(followerName: follwerName, userImagePath: imagePath)

	let mockConversationRepository = MockConversationRespositroy()
	// mockConversationRepository.expectation = expectation(description: "Waiting for result to uptdate")
	let userName = "Madhur"
	let viewModel = ConversationViewModel(userName: userName,
										  follower: followerListViewModel,
										  conversationRepository: mockConversationRepository)

	let message = Message(message: messageString,
		messageTo: followerListViewModel.followerName,
		messageFrom: userName,
		direction: .outgoing)
	viewModel.sendMessage(messagebody: messageString)
	XCTAssertEqual(viewModel.messages.value.count, 1)
	XCTAssertEqual(viewModel.messages.value[0], message)


class MockConversationRespositroy: ConversationRepositoryProtocol {

var messages: Observable<[Message]> = Observable([])

var expectation: XCTestExpectation?

var error: Error?

func sendMessage(message: Message, completion: @escaping (Result<Void, Error>) → Void) {

self.messages.value = [message]

if let error = error {


} else {






Sorry i am posting first time so formatting might be inappropriate