unit-testing

xunit - how to get HttpContext.User.Identity in unit tests

拜拜、爱过 提交于 2021-02-08 14:01:12
问题 I added a method to my controllers to get the user-id from the JWT token in the HttpContext . In my unit tests the HttpContext is null , so I get an exception. How can I solve the problem? Is there a way to moq the HttpContext ? Here is the method to get the user in my base controller protected string GetUserId() { if (HttpContext.User.Identity is ClaimsIdentity identity) { IEnumerable<Claim> claims = identity.Claims; return claims.ToList()[0].Value; } return ""; } One of my tests look like

xunit - how to get HttpContext.User.Identity in unit tests

笑着哭i 提交于 2021-02-08 14:01:04
问题 I added a method to my controllers to get the user-id from the JWT token in the HttpContext . In my unit tests the HttpContext is null , so I get an exception. How can I solve the problem? Is there a way to moq the HttpContext ? Here is the method to get the user in my base controller protected string GetUserId() { if (HttpContext.User.Identity is ClaimsIdentity identity) { IEnumerable<Claim> claims = identity.Claims; return claims.ToList()[0].Value; } return ""; } One of my tests look like

Make execution stop on error in RStudio / Interactive R session

不打扰是莪最后的温柔 提交于 2021-02-08 12:19:49
问题 When executing a block of code in RStudio, execution doesn't actually stop when an error occurs. For example, if I have the following code in the open editor: x <- 'test' stopifnot(is.numeric(x)) print('hello world') And run it (either with command-return or by clicking the "Run" button), it prints the error but then marches on and executes the print statement. Is there a way to configure RStudio to not proceed past the error? i.e. make it stop at line 2 above and not proceed to the print

Make execution stop on error in RStudio / Interactive R session

人盡茶涼 提交于 2021-02-08 12:18:53
问题 When executing a block of code in RStudio, execution doesn't actually stop when an error occurs. For example, if I have the following code in the open editor: x <- 'test' stopifnot(is.numeric(x)) print('hello world') And run it (either with command-return or by clicking the "Run" button), it prints the error but then marches on and executes the print statement. Is there a way to configure RStudio to not proceed past the error? i.e. make it stop at line 2 above and not proceed to the print

in python how to mock only the file write but not the file read?

坚强是说给别人听的谎言 提交于 2021-02-08 10:06:30
问题 I am testing a function in which both def foo(): with open('input.dat', 'r') as f: .... with open('output.dat', 'w') as f: .... are used. But I only want to mock the write part. Is it possible to do that? Or some other strategy should be used for testing such a function? with patch('__builtin__.open') as m: foo() would fail to read the data. Thanks in advance. 回答1: I found the following solution: from mock import patch, mock_open with open('ref.dat') as f: ref_output = f.read() with open(

in python how to mock only the file write but not the file read?

泪湿孤枕 提交于 2021-02-08 10:05:10
问题 I am testing a function in which both def foo(): with open('input.dat', 'r') as f: .... with open('output.dat', 'w') as f: .... are used. But I only want to mock the write part. Is it possible to do that? Or some other strategy should be used for testing such a function? with patch('__builtin__.open') as m: foo() would fail to read the data. Thanks in advance. 回答1: I found the following solution: from mock import patch, mock_open with open('ref.dat') as f: ref_output = f.read() with open(

C++ unit test - Test class with throwed error in constructor

半腔热情 提交于 2021-02-08 10:01:52
问题 I have a problem with testing class with member variable which is not dependent on me. I mean the class contain a variable that is include from other file. And this class throwing error in constructor(no matter why). And then I have a function that uses this variable. So how should I test this class? // ** classForMe.h can't modify ** class ClassForMe { public: ClassForMe(){ // not relevant what should be here but throw error in this case throw std::runtime_error(""); } int getData()const

Replacing C# method of declaring type which implements an interface and inherits from base

爱⌒轻易说出口 提交于 2021-02-08 08:38:36
问题 I'm trying to swap out the contents of a method at runtime for the purposes of unit testing legacy code. I've been working with these SO answers; Dynamically replace the contents of a C# method? How to replace the pointer to the overridden (virtual) method in the pointer of my method? (Release x64 and x86) Here's a full code sample of what I have so far. using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; namespace Foo.Bar { public interface

What are different types of test doubles and their uses?

孤街浪徒 提交于 2021-02-08 07:43:50
问题 I was going through an online course on test driven development and came across the concept of test doubles. As per the definition of test double in the course : Test Doubles : Test doubles are objects that are used in unit tests as replacement to the real production system collaborators. I got an idea what test doubles mean. But then it was mentioned there are various types of test doubles. The ones mentioned in the course were : Dummy : Objects that can be passed around as necessary but do

What are different types of test doubles and their uses?

风格不统一 提交于 2021-02-08 07:43:28
问题 I was going through an online course on test driven development and came across the concept of test doubles. As per the definition of test double in the course : Test Doubles : Test doubles are objects that are used in unit tests as replacement to the real production system collaborators. I got an idea what test doubles mean. But then it was mentioned there are various types of test doubles. The ones mentioned in the course were : Dummy : Objects that can be passed around as necessary but do