我在执行一些测试代码时遇到了一个非常奇怪的错误,测试的是一个不同的模块,该模块托管在一个独立项目中,但在Xamarin Studio的同一解决方案中。
该解决方案包含2个项目。一个模块项目和一个测试项目。测试项目通过项目引用引用模块项目。
//module-project/FluidLayout.fs
namespace iosautolayout
open System
module Fluidity =
type Init<'a> = Init of 'a
type Right<'a> = Right of 'a
type Left<'a> = Left of 'a
type HorizontalBuilder() =
member x.Yield(()) = Init "Init"
[<CustomOperation("part")>]
member x.Part(Init v, a) = Left "Left"
let horizontal = new HorizontalBuilder()
let create () =
let test_h = horizontal {
part "test"
}
test_h
然后我有一个包含2个文件的测试项目//测试项目/Tests.fs命名空间iolibrarytests
open System
open NUnit.Framework;
open iosautolayout.Fluidity
open MonoTouch.UIKit
open MonoTouch.Foundation
#nowarn "0025"
[<TestFixture>]
type Tests() =
[<Test>]
member me.``Create Structure with DSL``() =
let z = create ()
()
和AppDelegate启动所有
//test-project/AppDelegate.fs
namespace ioslibrarytests
open System
open MonoTouch.UIKit
open MonoTouch.Foundation
open MonoTouch.NUnit.UI
[<Register("AppDelegate")>]
type AppDelegate() =
inherit UIApplicationDelegate()
override val Window = null with get, set
override this.FinishedLaunching(app, options) =
this.Window <- new UIWindow(UIScreen.MainScreen.Bounds)
let runner = new TouchRunner(this.Window)
runner.Add (System.Reflection.Assembly.GetExecutingAssembly ());
this.Window.RootViewController <- new UINavigationController (runner.GetViewController ());
this.Window.MakeKeyAndVisible()
true
module Main =
[<EntryPoint>]
let main args =
UIApplication.Main(args, null, "AppDelegate")
0
这给了我以下错误
2015-02-15 15:11:46.902 ioslibrarytests[1017:14804]
[FAIL] Create Structure with DSL : System.NullReferenceException :
Object reference not set to an instance of an object
2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at
iosautolayout.Fluidity.create () [0x00000] in
/Users/projekte/pawo-pic/ios-autolayout/FluidLayout.fs:17
2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at
ioslibrarytests.Tests.Create Structure with DSL () [0x00000] in
/Users/projekte/pawo-pic/ios-library-tests/Tests.fs:17
2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at
(wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke
(System.Reflection.MonoMethod,object,object[],System.Exception&)
2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags
invokeAttr, System.Reflection.Binder binder, System.Object[] parameters,
System.Globalization.CultureInfo culture) [0x00044] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:230 201
我完全被这里发生的事情弄糊涂了。
非常感谢你的帮助。
我找到了一个解决方案,但现在我更困惑了。
如果我把horizontal
改成一个函数而不是一个val,那么一切都很好
namespace iosautolayout
open System
module Fluidity =
type Init<'a> = Init of 'a
type Right<'a> = Right of 'a
type Left<'a> = Left of 'a
type HorizontalBuilder() =
member x.Yield(()) = Init "Init"
[<CustomOperation("part")>]
member x.Part(Init v, a) = Left "Left"
let horizontal () = new HorizontalBuilder()
let create () =
let test_h = horizontal() {
part "test"
}
test_h
在交互式窗口中,我使用水平作为值还是函数都无关紧要。
有人能解释一下这种行为吗?