sandbox.restore() won't reset stub's called count - unit-testing

I'm completely new to Sinon/Jest and unit testing, so I'm kinda lost here. I tried to make a sandbox to declare all my stubs inside it but even after using sandbox.restore() the stub's call count is preserved, so my test fails in the next 'it'.
I wasn't able to stub TypeORM's objects directly, so I decided to create fake objects with only the methods I needed and made TypeORM's getRepository() use my created objects.
I'm not sure if this approach is even correct but looks like my tests are working, I can assert the number of calls and it's parameters, in the second 'it' I can expect that the error thrown equals 'Email já cadastrado' and if I change the expected message the test fails.
The issue is that the number of calls won't reset before the next 'it' block. So I always get an error on the line "sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades)" since it's being called twice (once on previous 'it' and a second time on the current block).
If I remove that line I get an error in the assert transaction section, since I expect commitTransaction to not have been called but it was called once (in the previous 'it' block).
Image of my Error
Here is my test:
const sandbox = sinon.createSandbox();
// Simulating TypeORM's QueryRunner
const fakeQueryRunner = {
connect: sandbox.stub().returnsThis(),
startTransaction: sandbox.stub().returnsThis(),
rollbackTransaction: sandbox.stub().returnsThis(),
commitTransaction: sandbox.stub().returnsThis(),
release: sandbox.stub().returnsThis(),
manager: { save: sandbox.stub().returnsThis() },
};
// Simulating TypeORM's Connection
const fakeConnection = {
createQueryRunner: sandbox.stub().returns( fakeQueryRunner ),
getRepository: sandbox.stub().returnsThis(),
findOneOrFail: sandbox.stub().returnsThis(),
}
// I've hidden the mock of my parameters/entities (Usuarios, Senhas, Cidades) since I don't think it's needed to solve the problem.
describe('UserRepository', function () {
let userRepository;
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
UserRepository,
],
}).compile();
userRepository = module.get<UserRepository>(UserRepository);
});
describe('signUp', function () {
beforeEach(function () {
// typeORM.getConnection() returns my object simulating a Connection.
sandbox.stub(typeorm, 'getConnection').returns( fakeConnection as unknown as typeorm.Connection )
// Stubbing this.create()
sandbox.stub(userRepository, 'create').returns( Usuarios )
});
afterEach(function () {
sandbox.restore();
});
it('successfully signs up the user', async function () {
// Simulating sucessful save transaction
fakeQueryRunner.manager.save.onCall(0).resolves(Usuarios);
fakeQueryRunner.manager.save.onCall(1).resolves(Senhas);
// Calling my method
await userRepository.signUp(mockCredentialsDto);
// First interation, this line works
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades);
// Asserting that transaction was commited
sandbox.assert.calledOnce(fakeQueryRunner.commitTransaction);
sandbox.assert.notCalled(fakeQueryRunner.rollbackTransaction);
});
it('throws a conflic exception as username already exists', async function () {
// Simulating a reject from transaction
fakeQueryRunner.manager.save.onCall(0).rejects({ code: '23505' });
// Calling my method and catching error
try {
await userRepository.signUp(mockCredentialsDto)
} catch (err) {
expect(err).toEqual(new Error('Email já cadastrado'));
}
// Second interation, this line giver ERROR (method called twice)
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades);
// Asserting that transaction was rolled back, this also gives an error since commitTransaction was called once (in the first 'it' block)
sandbox.assert.notCalled(fakeQueryRunner.commitTransaction);
sandbox.assert.calledOnce(fakeQueryRunner.rollbackTransaction);
});
// I will make another describe block here eventually
});
Here is the method I'm testing:
async signUp(authCredentialsDto: AuthCredentialsDto): Promise<signUpMessage> {
const { nome, email, genero, dataNascimento, profissao, organizacao, atuacao, nomeCidade, uf, senha } = authCredentialsDto;
const connection = getConnection();
const user = this.create();
const senhas = new Senhas;
const cidade = await connection.getRepository(Cidades).findOneOrFail({where: { nome: nomeCidade, uf: uf } });
// Set values
user.nome = nome;
user.email = email;
user.genero = genero;
user.dataNascimento = dataNascimento;
user.profissao = profissao;
user.organizacao = organizacao;
user.atuacao = atuacao;
user.idCidade = cidade;
const salt = await bcrypt.genSalt();
senhas.senha = await this.hashPassword(senha, salt)
senhas.idUsuario2 = user;
// Make a transaction to save the data
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await queryRunner.manager.save(user);
await queryRunner.manager.save(senhas);
await queryRunner.commitTransaction();
} catch (error) {
if ( error.code === '23505' ) { // Usuário repetido
await queryRunner.rollbackTransaction();
throw new ConflictException('Email já cadastrado')
} else {
await queryRunner.rollbackTransaction();
throw new InternalServerErrorException;
}
} finally {
await queryRunner.release();
}
let success: signUpMessage;
return success;
}

Managed to make it work, I declared my Sinon Sandbox and Objects inside a beforeEach instead of the start of my test.
My test looks like this now:
describe('UserRepository', () => {
let userRepository
let sandbox
let fakeQueryRunner
let fakeConnection
let mockUser: Usuarios
beforeEach(async () => {
sandbox = sinon.createSandbox()
// Cria um objeto QueryRunner fake
fakeQueryRunner = {
connect: sandbox.stub().returnsThis(),
startTransaction: sandbox.stub().returnsThis(),
rollbackTransaction: sandbox.stub().returnsThis(),
commitTransaction: sandbox.stub().returnsThis(),
release: sandbox.stub().returnsThis(),
manager: { save: sandbox.stub().returnsThis() },
}
// Cria um objeto Connection fake (note que o stub de createQueryRunner retorna o objeto fakeQueryRunner )
fakeConnection = {
createQueryRunner: sandbox.stub().returns(fakeQueryRunner),
getRepository: sandbox.stub().returnsThis(),
findOneOrFail: sandbox.stub().returnsThis(),
}
mockUser = {
idUsuario: '1',
atuacao: 'lol',
dataNascimento: '10/10/2021',
email: 'teste#kik.com',
genero: Genero.MASCULINO,
nome: 'Teste Nome',
organizacao: 'Teste org',
profissao: 'Teste Prof',
idCidade: mockCidade,
membros: [],
senhas: mockSenha,
validatePassword: sandbox.stub().returnsThis(),
}
const module = await Test.createTestingModule({
providers: [UserRepository],
}).compile()
userRepository = module.get<UserRepository>(UserRepository)
})
afterEach(() => {
sandbox.restore()
})
describe('signUp', () => {
beforeEach(function () {
// Cria um método falso que retorna o objeto fakeConnection
sandbox.stub(typeorm, 'getConnection').returns((fakeConnection as unknown) as typeorm.Connection)
// Simula o Create de UserRepository
sandbox.stub(userRepository, 'create').returns(Usuarios)
})
it('successfully signs up the user', async () => {
// Salvar Usuário e Senha foi bem sucedido
fakeQueryRunner.manager.save.onCall(0).resolves(Usuarios)
fakeQueryRunner.manager.save.onCall(1).resolves(Senhas)
await userRepository.signUp(mockCredentialsDto)
// Verificando instanciação do repositório
const call1 = fakeConnection.getRepository.onCall(0).resolves(Cidades)
sandbox.assert.calledWith(call1, Cidades)
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades)
// Verificando Transactions
sandbox.assert.calledOnce(fakeQueryRunner.commitTransaction)
sandbox.assert.notCalled(fakeQueryRunner.rollbackTransaction)
})
it('throws a conflic exception as username already exists', async () => {
// Houve um erro em um dos saves, resultando num rollback da transaction
fakeQueryRunner.manager.save.onCall(0).resolves(Usuarios)
fakeQueryRunner.manager.save.onCall(1).rejects({ code: '23505' })
try {
await userRepository.signUp(mockCredentialsDto)
} catch (err) {
expect(err).toEqual(new Error('Email já cadastrado'))
}
// Verificando instanciação do repositório
const call1 = fakeConnection.getRepository.onCall(0).resolves(Cidades)
sandbox.assert.calledWith(call1, Cidades)
sandbox.assert.calledOnceWithExactly(fakeConnection.getRepository, Cidades)
// Verificando Transactions
sandbox.assert.notCalled(fakeQueryRunner.commitTransaction)
sandbox.assert.calledOnce(fakeQueryRunner.rollbackTransaction)
})
})
})

Related

How to handle errors in websocket in jest end to end test?

I have one file which runs server on start. And there is one function to upgrade the connection based on some checks of the request.
//A.ts
export abstract class A{
protected _server: http.Server;
protected _wss: WebSocket.Server;
constructor(){
this._wss = new WebSocket.Server({ noServer: true });
}
async start(port: number) {
await this.startServer(port);
await this.startUpgardeListener();
await this.startWsConnectionListener();
}
private async startServer(port: number) {
this._server.listen(port, () => {
Logger.log(`Server started on port ` + port);
});
}
private async startUpgardeListener() {
this._server.on("upgrade", async (request: http.IncomingMessage, clientSocket, head: Buffer) => {
return await this.upgradeHandler(request, clientSocket as Socket, head);
});
}
private async startWsConnectionListener() {
this._wss.on('connection', async (clientWebSocket: WebSocket, request: http.IncomingMessage) => {
return await this.connectionHandler(clientWebSocket, request);
});
}
protected abstract upgradeHandler(request: http.IncomingMessage, clientSocket: Socket, head: Buffer): Promise<void>;
protected abstract connectionHandler(clientWebSocket: WebSocket, request: http.IncomingMessage): void;
}
//B.ts
export class FESProxy extends Proxy {
private _c = new H();
protected async upgradeHandler(request: IncomingMessage, clientTcpSocket: Socket, head: Buffer): Promise<void> {
let resource;
try{
this.initHandlers();
this.create(resource);
}
catch(e){
resource = this.cleanup(clientTcpSocket, resource);
}
}
private async create(resource){
resource = await this._c.createResource(resource);
if(resource.status === 400)
{
error = new Error(400);
throw error;
}
return true;
}
private initHandlers(resource: Resource, clientTcpSocket: Socket) {
this.initOnClose(clientTcpSocket, resource);
this.initOnError(clientTcpSocket, resource);
}
private initOnClose(clientTcpSocket: Socket, resource: Resource | undefined) {
clientTcpSocket.on('close', (err) => {
console.log(`Client TCP Socket Close Handler`);
resource = this.cleanup(clientTcpSocket, resource);
});
return resource;
}
private initOnError(clientTcpSocket: Socket, resource: Resource | undefined) {
clientTcpSocket.on('error', (e) => {
Logger.error(`Client TCP Socket Error Handler);
resource = this.cleanup(clientTcpSocket, resource);
});
return resource;
}
}
// B.spec.ts
let b : B;
let wb : WebSocket;
let workerServer : WebSocketServer;
describe("B test", () => {
beforeEach(async () => {
b = new B();
await b.start(3000);
})
afterEach(async () => {
wb.close();
workerServer.close();
b["_server"]?.close(()=>{});
});
it("Should handle 400 error in create call", async () => {
let resource = new Resource();
resource.status = 400;
const spiedOncreate = await jest.spyOn(b["_c"],"createResource").mockReturnValue(Promise.resolve(resource));
let uri = 'ws://localhost:3000/ws';
wb = new WebSocket(uri);
wb.on('error' ,(err) => {
console.log(err);
})
wb.on('close',() => {
})
wb.on('message',(e) =>{
console.log("Message in the socket ",e);
})
expect(spiedOncreate).toBeCalledTimes(1);
})
})
On running the test, I am getting this message
Expected number of calls: 1
Received number of calls: 0
Although I checked that catch block was called in the upgradeHandler method of B class with the status code of resource as 400. Which means that the createResource was called and with the status 400 which I set in the test only.
I also found that on error handler of websocket inside the test got triggered and got the message in the console
Error: Unexpected server response: 400
I am not able to see any other console logs if I put after assertion statement in the test. I guess my test is getting crashed and the error is not handled but not sure here. I added handlers in the test for websockets but still not working.
What am I missing or doing wrong to assert on the spyOn as I am not receiving the expected number of calls?

Cannot POST when sending request in Postman

I am having a small issue. I am trying to send a request in Postman, but I receive "
Cannot POST /api/logTemp/1/25
"
Here is my app.js:
const express = require('express')
const bodyParser = require('body-parser')
const cors= require('cors')
const fs= require('fs')
const path= require('path')
const morgan = require('morgan')
const router = require('./routes/route')
const app = express()
app.use(cors())
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false}))
// parse application/json
app.use(bodyParser.json())
app.use(morgan('dev'))
//create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(path.join(__dirname, '/logs/access.log'), {flags: 'a'})
//setup the logger
app.use(morgan('combined', {stream: accessLogStream}))
app.use(router)
app.get('/', (req, res) => {
res.send('Hello World!')
})
const port = 3000
//app.listen(process.env.PORT || port, (err) => {
app.listen(port, () => {
console.log('Server started running on :' + port)
})
and here is my controller file:
const { getEnabledCategories } = require('trace_events');
const mysql = require('../database/db')
class MainController {
async logTemp(req, res){
console.log(req.params.temperature)
console.log(req.params.deviceID)
if(req.params.deviceID != null && req.params.temperature != null){
let deviceID = req.params.deviceID
let temperature = req.params.temperature;
var sql = `insert into log_temp (log_date, device_id, temp) values (now(),${deviceID}, ${temperature});`
mysql.query(sql, (error,data,fields) => {
if(error){
res.status(500)
res.send(error.message)
} else{
console.log(data)
res.json({
status: 200,
message: "Log uploaded successfully",
affectedRows: data.affectedRows
})
}
})
} else {
res.send('Por favor llena todos los datos!')
}
}
async getLogs(req, res){
console.log("Get Logs")
console.log(req.params.deviceID)
if(req.params.deviceID!=null){
let deviceID = req.params.deviceID;
var sql = `SELECT * FROM log_temp where device_id=${deviceID}`
mysql.query(sql, (error, data, fields) => {
if(error) {
res.status(500)
res.send(error.message)
} else {
console.log(data)
res.json({
data
})
}
})
}
}
}
const tempController = new MainController()
module.exports = tempController;
The code above was made in Visual Studio. It is odd because getLogs does work but logTemp does not. What I intend to do with logTemp is add a new value (which is the value temperature) to MySQL database. The connection to the database worked just fine, as well as localhost. If you need any more info in order to help me find a solution, please let me know and I will be more than happy to provide it. Also, i'm sorry for any grammar errors, english is not my first language :)

Ethereum Hardhat Testing Expect Not Catching Revert Reason

I'm using Hardhat framewrok and doing some testing with an erc20 contract:
it("testing with second", async () => {
const amount = ethers.utils.parseEther("1");
await expect(
ERC20Contract.connect(second).transferFrom(
ERC20Contract.address,
second,
amount
)
).to.be.revertedWith("ERC20: insufficient allowance");
});
I'm expecting the contract to revert with an error of : ERC20: insufficient allowance, and this is the error I'm getting:
Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"name":"ProviderError","_stack":"ProviderError: HttpProviderError\n at HttpProvider.request (/home/kadiemq/hh-tut/erc20-tut/node_modules/hardhat/src/internal/core/providers/http.ts:78:19)\n at LocalAccountsProvider.request (/home/kadiemq/hh-tut/erc20-tut/node_modules/hardhat/src/internal/core/providers/accounts.ts:187:34)\n at processTicksAndRejections (node:internal/process/task_queues:96:5)\n at async EthersProviderWrapper.send (/home/kadiemq/hh-tut/erc20-tut/node_modules/#nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)","code":-32000,"_isProviderError":true,"data":{"stack":"RuntimeError: VM Exception while processing transaction: revert ERC20: insufficient allowance\n at Function.RuntimeError.fromResults (/tmp/.mount_ganachAQrIFW/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n at module.exports (/tmp/.mount_ganachAQrIFW/resources/static/node/node_modules/ganache-core/lib/utils/gas/guestimation.js:142:32)","name":"RuntimeError"}}, tx={"data":"0x23b872dd000000000000000000000000624daf7e06c04e0ab541323b3d3e95b629745a6000000000000000000000000033757dfeda24de8dc2b46d348a035ad60bbc3a3f0000000000000000000000000000000000000000000000000de0b6b3a7640000","to":{},"from":"0x2c93fc47DC6aaF30CD5a6C47F59bD898842B0190","gasPrice":{"type":"BigNumber","hex":"0x04a817c800"},"type":0,"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE_GAS_LIMIT, version=abstract-signer/5.7.0)
in the error message I can see ERC20: insufficient allowance but the expect function is not getting it and failing the test.
Here is the contract:
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract OurToken is ERC20 {
constructor(uint256 initialSupply) ERC20("OurToken", "OT") {
_mint(address(this), initialSupply);
_approve(address(this), msg.sender, initialSupply);
}
}
Deploy:
import { DeployFunction } from "hardhat-deploy/dist/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import {
developmentChains,
INITIAL_SUPPLY,
networkConfig,
} from "../helper-hardhat-config";
const DeployToken: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { getNamedAccounts, deployments, network } = hre;
const { deploy, log } = deployments;
const { deployer } = await getNamedAccounts();
const chainId = network.config.chainId!;
const args = [INITIAL_SUPPLY];
const OurToken = await deploy("OurToken", {
from: deployer,
args: args,
log: true,
waitConfirmations: networkConfig[network.name].blockConfirmations || 1,
});
};
export default DeployToken;
DeployToken.tags = ["all", "token"];
Test:
import { deployments, ethers, getNamedAccounts } from "hardhat";
import { ERC20 } from "../typechain-types";
import { assert, expect } from "chai";
describe("Testing", async () => {
let deployer: string;
let second: string;
let ERC20Contract: ERC20;
beforeEach(async () => {
deployer = (await getNamedAccounts()).deployer;
second = (await getNamedAccounts()).second;
await deployments.fixture(["all"]);
});
it("testing with deployer", async () => {
ERC20Contract = await ethers.getContract("OurToken", deployer);
const amount = ethers.utils.parseEther("1");
const previousBalance = await ERC20Contract.balanceOf(deployer);
const tx = await ERC20Contract.transferFrom(
ERC20Contract.address,
deployer,
amount
);
tx.wait(1);
const newBalance = await ERC20Contract.balanceOf(deployer);
assert.equal(
previousBalance.add(amount).toString(),
newBalance.toString()
);
});
it("testing with second", async () => {
ERC20Contract = ERC20Contract.connect(second);
const amount = ethers.utils.parseEther("1");
await expect(
ERC20Contract.transferFrom(ERC20Contract.address, deployer, amount)
).to.be.revertedWith("ERC20: insufficient allowance");
});
});
So after almost a day of debugging it turns out that I need another plugin to use revertedWith() I need to install npm install --save-dev #nomiclabs/hardhat-waffle 'ethereum-waffle#^3.0.0' #nomiclabs/hardhat-ethers 'ethers#^5.0.0' and import it in the config file, otherwise I need to use rejectedWith("ERC20: insufficient allowance").
if anyone can explain this further it would be better because still I don't whats the difference between revertedWith and rejectedWith and why we need to install hardhat-waffle to be able to use revertedWith.
more info about hardhat-waffle in this page: https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-waffle

Assertion error whilst running truffle test

I got an error while running truffle test on a smart contract, can anyone help me with this?
const KryptoBird = artifacts.require("KryptoBird");
// check for chai
require('chai')
.use(require('chai-as-promised'))
.should()
contract('KryptoBird', (accounts) => {
let contract
before( async () => {
contract = await KryptoBird.deployed()
})
describe('deployment', async() => {
it("deploys successfully", async () => {
const address = contract.address;
assert.notEqual(address, '')
assert.notEqual(address, null)
assert.notEqual(address, undefined)
assert.notEqual(address, 0x0)
})
it('has a name', async() => {
const name = await contract.name()
assert.equal(name, 'KryptoBird')
})
it('has a symbol', async() => {
const symbol = await contract.symbol()
assert.equal(symbol, 'KBIRDZ')
})
})
describe('minting', async ()=> {
it('creates a new token', async ()=> {
const result = await contract.mint('https...1')
const totalSupply = await contract.totalSupply()
assert.equal(totalSupply, 1)
const event = result.logs[0].args
assert.equal(event._from, '0x0000000000000000000000000000000000000000', 'from is the contract')
assert.equal(event._to, accounts[0], 'to is msg.sender')
await contract.mint('https...1').should.be.rejected
})
})
})
this is my code, and when I run it, it gives me an error that I don't understand
this is the error:
1 ) Contract : KryptoBird
minting
creates a new token :
AssertionError : expected promise to be rejected but it was fulfilled with { Object ( tx , receipt , ... ) }
error message
please help me, I've been stuck on this for a day.
heres my smart contract code, if u could help me
thanks a lot! :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import './ERC721Connecter.sol';
contract KryptoBird is ERC721Connecter {
string[] public kryptoBirdz;
mapping(string => bool) _kryptoBirdzExists;
function mint(string memory _kryptoBird) public {
require(!_kryptoBirdzExists[_kryptoBird],
'Error - kryptoBird already exists');
// this is deprecated - uint _id = KryptoBirdz.push(_kryptoBird);
kryptoBirdz.push(_kryptoBird);
uint _id = kryptoBirdz.length - 1;
// .push no logner returns the length but a ref to the added element
_mint(msg.sender, _id);
}
constructor() ERC721Connecter('KryptoBird','KBIRDZ')
{}
}
The problem is not you are not adding entry into the _kryptoBirdzExists mapping. before you mint, you are checking this condition
require(!_kryptoBirdzExists[_kryptoBird],
So after minting, you should be updating the mapping
_kryptoBirdzExists[_kryptoBird]=true;
Your test is not failing because you never added the 'https...1' to the mapping so require stament was passing so you were minting again

How to mock 'FirebaseAuth.instance'?

I want to start writing unit test to my flutter app that have developed using GetX pattern. In GetX pattern, the code is separated to controller and view, so all methods that I want to test is in controller.
In my app, I am using firebase to make authentication with mobile number.
This is LoginController:
class LoginController extends GetxService {
...
LoginController(this.authService);
final _auth = FirebaseAuth.instance;
String validatePhoneNumber(String phoneNumber) {
if (!phoneNumber.startsWith('+20')) {
return 'أدخل كود البلد مثل: +20 في مصر';
} else if (phoneNumber.length < 11) {
return 'أدخل رقم صحيح';
} else if (phoneNumber.isEmpty) {
return 'أدخل رقم الهاتف';
}
return null;
}
Future<void> validate() async {
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumberController.text,
timeout: timeoutDuration,
codeAutoRetrievalTimeout: (String verificationId) {
verId.value = verificationId;
currentState.value = SignInPhoneWidgetState.CodeAutoRetrievalTimeout;
},
codeSent: (String verificationId, [int forceResendingToken]) {
verId.value = verificationId;
currentState.value = SignInPhoneWidgetState.CodeSent;
DialogService.to.stopLoading();
Get.toNamed(Routes.ACCEPT_SMS);
},
verificationCompleted: (AuthCredential phoneAuthCredential) async {
currentState.value = SignInPhoneWidgetState.Complete;
try {
if (authService.currentUser.value != null) {
await authService.currentUser.value
.linkWithCredential(phoneAuthCredential);
} else {
await _auth.signInWithCredential(phoneAuthCredential);
}
Get.offAllNamed(Routes.ROOTHOME);
//widget.onLoggedIn(authResult);
} on PlatformException catch (e) {
print(e);
errorCode.value = e.code;
errorMessage.value = e.message;
} catch (e) {
print(e);
} finally {
//.......
}
},
verificationFailed: (FirebaseAuthException error) {
errorCode.value = error.code;
errorMessage.value = error.message;
currentState.value = SignInPhoneWidgetState.Failed;
},
//forceResendingToken:
);
}
Future<void> validateSmsCode() async {
//_auth.
var cred = PhoneAuthProvider.credential(
verificationId: verId.value, smsCode: verifyCodeController.text);
try {
if (authService.currentUser.value != null) {
await authService.currentUser.value.linkWithCredential(cred);
} else {
await _auth.signInWithCredential(cred);
await Get.offAllNamed(Routes.ROOTHOME);
}
} on PlatformException catch (ex) {
errorCode.value = ex.code;
errorMessage.value = ex.message;
currentState.value = SignInPhoneWidgetState.Failed;
} on FirebaseAuthException catch (ex) {
errorCode.value = ex.code;
errorMessage.value = ex.message;
currentState.value = SignInPhoneWidgetState.Failed;
}
}
...
}
This is login_test.dart file:
I should mock every outside operation like firebase. But In this case I want to test validatePhoneNumber method, that checks if the phone number is valid or not. the method it self hasn't firebase operations. But, the method is called by a LoginController object, And this object it self has instance of FirebaseAuth.instance.
final _authSerive = AuthService();
main() async {
final loginController = LoginController(_authSerive);
setUp(() async {});
tearDown(() {});
group('Phone Validation', () {
test('Valid Email', () {
String result = loginController.validatePhoneNumber('+201001234567');
expect(result, null);
});
});
}
When I tried to run this test method, This error appeared.
Failed to load "D:\bdaya\ta7t-elbeet-client\test\login_test.dart":
[core/no-app] No Firebase App '[DEFAULT]' has been created - call
Firebase.initializeApp()
The reasen is:
This line in The LoginController:
final _auth = FirebaseAuth.instance;
I certainly know that I have to mock Firebase operations.
How to mock it in this case or, What should I do?