Posts

Showing posts with the label testing

How to use a factory within a factory with Laravel?

Image
Clash Royale CLAN TAG #URR8PPP How to use a factory within a factory with Laravel? I have a factory creating StreetAddresses and I want to use it in my factory that creates CreditCards. GOOD: This behaves as expected, creating a CreditCard object with an stdClass for the street_address and the properties are all good. <?php use FuquIoLaravelAccountingOrmCreditCard; use FuquIoLaravelCommonRelatablesLocatableStreetAddress; use FuquIoLaravelUserAccessUser; $factory->define(CreditCard::class, function (FakerGenerator $faker){ $street_address = factory(StreetAddress::class)->make(); return [ 'customer_id' => User::root()->getKey(), 'cardholder' => $faker->firstName . ' ' . $faker->lastName, 'nick_name' => $faker->company, // LOOK HERE: this works 'street_address' => function () use ($street_address) { return (object) $street_address->toArray(); } ]; }); NOT so good: This returns null ...

Mock imports to test function logic

Image
Clash Royale CLAN TAG #URR8PPP Mock imports to test function logic Lets say I have the following file. This file imports promiseFunction from a library and I'd like to test the doSomething() function. promiseFunction doSomething() In particular, assert the states when the promise resolves or fails // file: MyComponent.js import promiseFunction from 'somewhere'; class MyClass extends Component { doSomething = () => { promiseFunction() .then((data) => { this.setState({...this.state, name: data.name}); }.catch(() => { this.setState({...this.state, error: 'error'}); }); } } How do I mock the promiseFunction thats being imported. Or really just any function that's being imported. promiseFunction // file: MyClass.spec.js it('sets error in state when doSomething() is rejected', () => { const wrapper = shallow(<MyClass />); // How do I mock promiseFunction here? wrapper.instance().doSomething()...

What is the default test framework for ReactJS?

Image
Clash Royale CLAN TAG #URR8PPP What is the default test framework for ReactJS? By using Create-React-App to scaffolding a new project, it will include default App.test.js with below content: Create-React-App App.test.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; it('renders without crashing', () => { const div = document.createElement('div'); ReactDOM.render(<App />, div); //expect(div.innerHTML).toContain('Hi There!'); ReactDOM.unmountComponentAtNode(div); }); I've tried to run command yarn test , below is the response i got yarn test Error: Cannot find module '/Users/isaaclem/Code/ReactJS/testing/node_modules/jest-cli' And if I try npm run test instead, I'm hitting same error too, which saying couldnt find jest-cli . Is there any reason where C-R-A gives us a default test file but not including the necessary framework/module? npm run test jest-cli C-R-A Belo...

How does one test net.Conn in unit tests in Golang?

Image
Clash Royale CLAN TAG #URR8PPP How does one test net.Conn in unit tests in Golang? I'm currently looking into creating some unit tests for net.Conn interface in Go, as well as other functions that build up on top of that functionality, and I'm wondering what is the best way to unit test that in Google Go? My code looks like: conn, _:=net.Dial("tcp", "127.0.0.1:8080") ... fmt.Fprintf(conn, "test") ... buffer:=make(byte, 100) conn.Read(buffer) Is the most efficient way of testing this code and the code that uses these functions to spin up a separate goroutine to act like the server, use net.http.httptest package, or something else? Suggest reading the source for the tests for the actual net library. I have picked up plenty of tips by doing that in the past. Secondly as you have already mentioned use the httptest package. – miltonb Jun 7 '15 at 4:31 ...