Click here to Skip to main content
16,017,241 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public class BaseHeaderRequest
{
    public string MarketId { get; set; }
    public string StoreId { get; set; }
    public string DataVersion { get; set; }
    public string AcceptLanguage { get; set; }

    public override bool Equals(object obj) =>
        obj is BaseHeaderRequest headers && //This line showing red in Sonarqube. Partially covered by tests(5 of 6 conditions)
        MarketId == headers.MarketId &&
        StoreId == headers.StoreId &&
        AcceptLanguage == headers.AcceptLanguage;

    public override int GetHashCode() =>
        HashCode.Combine(MarketId, StoreId, AcceptLanguage);
}


What I have tried:

I have written few tests to cover the null object compare, different object compare, same type objects with different data.
Posted
Updated 5-Apr-23 20:07pm
Comments
Chris Copeland 28-Mar-23 4:40am    
What exactly is Sonarqube saying? Is it saying that the condition isn't always tested? If so, you might need to consider writing a unit test that covers false values. Ie. pass in a null into the method, and also pass an object that isn't an instance of BaseHeaderRequest.
Virendra S from Bangalore, Karnataka 28-Mar-23 5:13am    
Yes, it is saying that always the condition is not tested. already wrote few tests cases to cover this scenario.

[Fact]
public void Equals_WithNull_ShouldReturnFalse()
{
// Arrange
var headers = new BaseHeaderRequest();

// Act
var result = headers.Equals(null);

// Assert
result.Should().BeFalse();
}



[Fact]
public void EqualsShouldReturnFalseWhenDifferentObjectComparedWithBaseHeaderRequestObject()
{
//Arrange
var someObj = new List<string>();
var objBaseHeaderRequest = new BaseHeaderRequest();

//Act
var result = objBaseHeaderRequest.Equals(someObj);

//Assert
result.Should().BeFalse();
}

1 solution

C#
var object1 = new BaseHeaderRequest();
object1.MarketId = "MarketIdAbc";
object1.StoreId = "10986586";
object1.AcceptLanguage = "en";

string sJson = JsonConvert.SerializeObject(object1);
var object2 = JsonConvert.DeserializeObject<BaseHeaderRequest>(sJson);
Assert.IsTrue(object1.equals(object2));


You should make instance of BaseHeaderRequest and make equals both values at MarketId, StoreId, and AcceptLanguage
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900