0

I'm trying try to write my first NSpec test in a Mobile Service application. I created an attribute in the spec. But when I try to access that element on the next line I can't access the public properties on the instance because Visual Studio is not recognizing the variable.

AppointmentSpec.cs

public class AppointmentSpec : nspec
    {
        private AppointmentDTO _dto = new AppointmentDTO();
        _dto.PatientId = "124234"; // Visual Studio is not regonizing _dto

    }

AppointmentDTO.cs

public class AppointmentDTO
    {
        public string PatientId { get; set; }
       // public string PathwayId { get; set; }
        public string ItemId { get; set; }
        public string Subject { get; set; } //Dr. Visit, labtest, labtest name, follow up, other...
       // public string ProviderName { get; set; }
        public string Location { get; set; }  //clinic, hsopital name, etc
        public string Address { get; set; }  //street address
        public string PhoneNumber { get; set; }
        //to automatically add a corresponding appointment to the provider's calendar
        public bool SetProviderAppointment { get; set; }
        public string ProviderItemId { get; set; }
        public List<string> ProviderItemIds { get; set; } 
        public bool IsVideoCall { get; set; }
        public TimeSpan StartTime { get; set; }
        public TimeSpan EndTime { get; set; }

        public DateScheduleInfo EventDateSchedule { get; set; }

        //public TimeScheduleInfo EventTimeSchedule { get; set; }

        //public void Send(object target, string methodName, params object[] args)
        //{
        //    var properties = GetProperties(target.GetType());
        //    var property = properties.First(p => p.Name == methodName);
        //    if(property == null)
        //        throw new ArgumentException($"{target.GetType()} has no property or method ");
        //    property.SetValue(target, args.First());
        //}

        //private static IEnumerable<PropertyInfo> GetProperties(Type t)
        //{
        //    return t == null ? Enumerable.Empty<PropertyInfo>() : t.GetProperties().Union(GetProperties(t.BaseType));
        //}
    }
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
  • 5
    You can't write code outside a function. – SLaks Jan 05 '17 at 18:17
  • 1
    If you only want initialize _dto you could use `private AppointmentDTO _dto = new AppointmentDTO() { PatientId = "124234"};` It sets the property (PatientId) during initialization. – H.G. Sandhagen Jan 05 '17 at 18:23

2 Answers2

4

Do the assignment inside the constructor of the class:

public class AppointmentSpec : nspec
    {
        private AppointmentDTO _dto = new AppointmentDTO();

        public AppointmentSpec()
        {
            _dto.PatientId = "124234";
        }

    }
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
4

You're accessing _dto in an invalid context. If you meant to initialize PatientId with some data, try this:

private AppointmentDTO _dto = new AppointmentDTO
{
    PatientId = "124234"
};

See MSDN

haim770
  • 48,394
  • 7
  • 105
  • 133