How to test this function?
public function destroy(Post $post)
{
$post->delete();
return Response::json([], ResponseHttp::HTTP_OK);
}
I know that I must create several post delete it, but how it test?
How to test this function?
public function destroy(Post $post)
{
$post->delete();
return Response::json([], ResponseHttp::HTTP_OK);
}
I know that I must create several post delete it, but how it test?
I'm currently using Flask-HTTPAuth for basic authentication within my project. I've tested it by hand using curl and base64 tokens and it does indeed work. However I'm running into problems creating tests proving it works. This is my current test and it always turns back 401:
class TestLoginApi
def setup(self):
myapp.app.config.from_object("config.TestingConfig")
self.app = myapp.app.test_client()
client = MongoClient(myapp.app.config['DATABASE_URL'])
db = client.get_default_database()
assert list(db.collection_names()) == []
db.users.insert_one({'name': 'testuser', 'password': 'testpassword'})
def teardown(self):
client = MongoClient(myapp.app.config['DATABASE_URL'])
client.drop_database(client.get_default_database())
def test_not_logged_in(self):
rv = self.app.get('/api/v1/login/')
assert rv.status_code == 401
def test_correct_password(self):
d = Headers()
d.add('Authorization', 'Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk')
rv = self.app.get('/api/v1/login/', headers=d,
content_type='application/json')
assert rv.status_code == 200
I've also tried changing the following:
def test_correct_password(self):
d = Headers()
d.add('Authorization', 'Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk')
rv = self.app.get('/api/v1/login/', headers=d,
content_type='application/json')
assert rv.status_code == 200
to this with no success:
def test_correct_password(self):
rv = self.app.get('/api/v1/login/', headers={"Authorization": "Basic {user}".format(user=base64.b64encode(b"testuser:testpassword"))})
assert rv.status_code == 200
I never write tests for my rest controllers and services. I have read offical docs , how to write integration tests in spring boot. So for example I have a Rest Controller class
@RestController
@RequestMapping(value = "users")
public class SvcUser {
@RequestMapping(value = "user", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public ResponseEntity<String> registrateUser(@RequestBody Registration registrate) throws TimeoutException, SocketTimeoutException {
final String result = userService.registrateUser(registrate.getPhoneCode(), registrate.getPhoneNumber(), registrate.getOsType());
return ResponseEntity.ok(result);
}
And inside userServiceClass i did smth like this
registrateUser(String args ...){
Users user = new User();
user.setSmth(smth);
userRepository.save(user)
}
My integration test
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,classes = GmoikaApiApplication.class)
public class GmoikaApiApplicationTests {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setUp(){
this.mockMvc = webAppContextSetup(wac).build();
}
@Test
public void firstTest() throws Exception {
mockMvc.perform(post("/users/user")
.content("my new user"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.error",is(0)))
.andDo(print())
.andReturn();
}
And this is work fine, but i have new user in my db. I don't wanna create fake users in production only due to tests.My question is how to avoid creation of new users in db inside integration tests?
Many a times in Interview, the interviewer as me this question. Can any one help me how to answer this question?
I have a Spring Boot project with Hibernate. I would like to use 2 separate hibernate.properties files: one for normal use of application, another only for test.
So, how can I do that? Or maybe it is not possible? I don't want to comment/uncomment lines in this file for tests.
Thanks for every answers and help.
#hibernate.connection.driver_class = org.postgresql.Driver
#hibernate.connection.url = jdbc:postgresql://localhost:5432/dummydb
#hibernate.connection.username = postgres
#hibernate.connection.password = postgres
#hibernate.dialect = org.hibernate.dialect.PostgreSQL91Dialect
hibernate.connection.driver_class = org.hsqldb.jdbcDriver
hibernate.connection.url = jdbc:hsqldb:mem:dummydb
hibernate.connection.username = user
hibernate.connection.password = password
hibernate.hbm2ddl.auto=create
hibernate.dialect = org.hibernate.dialect.HSQLDialect
hibernate.show_sql=true
I installed node and lodash and several other libraries
npm install grunt-contrib-jshint --save-dev
npm install grunt-contrib-testem --save-dev
npm install sinon --save-dev
npm install -g phantomjs
npm install lodash --save
npm install jquery --save
npm install jasmine-node --save-dev
But when I tried to run the jasmine test, one of two things happens:
This is an screenshot of my code and test. As you can see, if I add the _ reference, the result is
If I remove that, the result is
I'm writing some tests and want to share setUp and tearDown methods between different TestCase classes. To do this I figured you can use a base test class which only implements the setUp and tearDown methods and inherit from it. However, I also have situations where I'd like to use variables from multiple setUp's. Here's an example:
class Base(unittest.TestCase):
def setUp(self):
self.shared = 'I am shared between everyone'
def tearDown(self):
del self.shared
class Base2(unittest.TestCase):
def setUp(self):
self.partial_shared = 'I am shared between only some tests'
def tearDown(self):
del self.partial_shared
class Test1(Base):
def test(self):
print self.shared
test_var = 'I only need Base'
print test_var
class Test2(Base2):
def test(self):
print self.partial_shared
test_var = 'I only need Base2'
class Test3(Base, Base2):
def test(self):
test_var = 'I need both Base and Base2'
print self.shared
print self.partial_shared
if __name__=='__main__':
unittest.main()
Here is the output:
..EI am shared between everyone
I only need Base
I am shared between only some tests
I am shared between everyone
======================================================================
ERROR: test (__main__.Test3)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/b3053674/Documents/PyCoTools/PyCoTools/Tests/base_tests.py", line 134, in test
print self.partial_shared
AttributeError: 'Test3' object has no attribute 'partial_shared'
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (errors=1)
Is it possible to implement a class heirachy such as this?