[英]Testing Code in C# Visual Studio 2013 (Method Name Does Not Exist In Current Context)
I am trying to test some methods but it is saying "The name [method] does not exist in the current context." 我正在尝试测试一些方法,但是它说“名称(方法)在当前上下文中不存在”。
To test the methods AddUnsynchronizedPing()
, GetUnsynchronizedPings()
, and SetAllPingsSynchronized()
I had to create a Ping object, which I did. 为了测试方法AddUnsynchronizedPing()
, GetUnsynchronizedPings()
和SetAllPingsSynchronized()
我必须创建一个Ping对象。
The methods are from my DataManager
class: 这些方法来自我的DataManager
类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using BB.Mobile.Models;
using SQLite;
namespace BB.Mobile
{
/// <summary>
/// A class to provide a single interface for interacting with all SQLite data operations for stored tracking points.
/// </summary>
///
class DataManager
{
private SQLiteConnection db = null;
public DataManager()
{
if (this.db == null)
{
string dbPath = Path.Combine(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
"bb.db3");
db = new SQLiteConnection(dbPath);
db.CreateTable<Ping>();
db.CreateTable<PingGroup>();
}
}
/// <summary>
/// Will compile and return all matching unsynchronized ping data from the SQLite database.
/// </summary>
/// <returns></returns>
public List<PingGroup> GetUnsynchronizedPings()
{
List<PingGroup> unsynchronizedPings = new List<PingGroup>();
// TODO: Retrieve all unsynchronized pings from the SQLite database and return them to the caller.
//var pGroup = db.Get<PingGroup>();
//var pGroupList = db.List<PingGroup>();
var pGroups = db.Table<PingGroup>();
foreach (var pGroup in pGroups)
{
var pings = db.Query<Ping>("select * from Pings where PingGroupID = " + pGroup.ID.ToString());
pGroup.Pings = pings;
}
return unsynchronizedPings;
}
/// <summary>
/// Insert a single ping group into the SQLite ping database.
/// </summary>
/// <param name="pingGroup"></param>
public void AddUnsynchronizedPing(PingGroup pingGroup)
{
// TODO: Add the passed ping group parameter into the SQLite database as new/unsynchronized.
if (pingGroup != null)
{
// Add ping group to the database.
db.Insert(pingGroup);
foreach (var ping in pingGroup.Pings)
{
ping.PingGroupID = pingGroup.ID;
}
db.InsertAll(pingGroup.Pings);
}
}
/// <summary>
/// Mark all open and unsynchronized pings in the database as synchronized.
/// </summary>
public void SetAllPingsSynchronized()
{
db.DeleteAll<PingGroup>();
db.DeleteAll<Ping>();
}
}
}
Where I am testing said methods is in the OnCreate
method of my in my MainActivity
class. 我正在测试的方法位于MainActivity
类中的OnCreate
方法中。 Here is said method: 这里说方法:
protected override void OnCreate(Bundle bundle)
{
Ping p = new Ping()
{
Latitude = +0.000000,
Longitude = -90.000000,
DateCreated = DateTime.UtcNow,
};
AddUnsynchronizedPing(p);
GetUnsynchronizedPings(p);
SetAllPingsSynchronized(p);
GetUnsynchronizedPings(p);
base.OnCreate(bundle);
this.userSettings = new Settings(this.ApplicationContext);
//this.setLayoutToSettings();
if (this.userSettings.isUserLoggedin()) // check for logged in user to switch between layouts
{
this.setLayoutToSettings();
InitializeAlarms();
}
else
{
this.setLayoutToLogin();
}
}
As implied above, the methods AddUnsynchronizedPing()
, GetUnsynchronizedPings()
, and SetAllPingsSynchronized()
in the MainActivity
class are returning the "The name [method] does not exist in the current context" error. 如上所述, MainActivity
类中的方法AddUnsynchronizedPing()
, GetUnsynchronizedPings()
和SetAllPingsSynchronized()
返回“名称[方法]在当前上下文中不存在”错误。
You need to create an instance of DataManager first (you also need to be sure DataManager is public) 您需要首先创建DataManager的实例(还需要确保DataManager是公共的)
DataManager mgr = new DataManager();
// this method requires a PingGroup argument, so you need to instantiate and pass one
PingGroup pg = new PingGroup();
mgr.AddUnsynchronizedPing(pg);
var pings = mgr.GetUnsynchronizedPings();
mgr.SetAllPingsSynchronized();
将DataManager类公开。
public class DataManager
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.