2012-03-22 58 views
1

我嘲笑VSTO對象,並在一個項目中(我沒有寫),它有這個代碼:嘲笑的對象不具有智能感知顯示的所有屬性 - 在一個項目中,但有他們在其他

var listOfSheets = new List<Worksheet>(); 
var mockSheets = Substitute.For<Sheets>(); 
mockSheets.Count.Returns(listOfSheets.Count); 

智能感知工具提示爲mockSheets顯示6個性質:

NSubstitute All Properties

與破發點線在這個項目工作。

不過,我有一個不同的項目爲mockSheets相同的代碼(相同的標記,命名空間等),但智能感知工具提示只顯示1個屬性:

NSubstitute Only one Property

我知道這是我想要解決的,但根本原因僅供參考實際的問題是:

Cannot perform runtime binding on a null reference

Cannot perform runtime binding on a null reference

編輯:

所述片狀物體是嘲笑:

public static Worksheet Sheet 
{ 
    get 
    { 
     var mockSheet = Substitute.For<Worksheet>(); 
     mockSheet.Name = MockSheetName; 
     mockSheet.Visible = XlSheetVisibility.xlSheetVisible; 

     return mockSheet; 
    } 
} 

public static Workbook Workbook() 
{ 
    return Workbook(1); 
} 
+1

如果手動模擬'Sheets'(創建,從'Sheets'派生的'FakeSheets'類),它的工作原理如預期? – 2012-03-22 21:32:40

+1

@DavidTchepak嗨大衛,謝謝你在這一點上接我,是的Sheets已經是假的。請參閱我的編輯。我認爲有一個潛在的原因,屬性沒有顯示,你知道任何調試技術發現?其實我會下載源代碼NSubstitute和使用,而不是釋放DLL – 2012-03-22 21:34:53

+1

@DavidTchepak我沒有管理員權限(工作),我不能安裝的NuGet :(我不甚至有反光,所以我不能甚至反編譯的DLL :(請問你知道任何其他方式來獲得源代碼? – 2012-03-22 22:15:02

回答

1

一些歷史:

我得到這些錯誤編譯問題:

1.Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
2.One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

所以我發現這篇文章,並參考了Microsoft.CSharp庫: C# 4.0 and .Net 3.5

要點: 我一直在顯示的類嘲笑Excel對象模型,我將這個類從一個項目複製到另一個項目(我不能將其他項目作爲其實際單獨的項目加上它會導致循環依賴)我發現返回擴展方法列在intellisense中,但編譯時出現「無法解析符號」。儘管定義在兩個類/項目中都是一樣的。爲了解決這個問題起初我並註釋掉行:

public static Range Cell 
{ 
    get 
    { 
     var mockCell = Substitute.For<Range>(); 
     mockCell.Address.Returns("$A$1"); 
     mockCell.Formula = "=1+1"; 
     mockCell.ToString().Returns(mockCell.Formula.ToString()); 
     //mockCell.ToString().Returns(info => mockCell.Formula.ToString()); 
     //SubstituteExtensions.Returns(mockCell.ToString(), mockCell.Formula.ToString()); 
     mockCell.Worksheet.Returns(Sheet); 
     mockCell.Worksheet.Name.Returns(MockSheetName); 

     return mockCell; 
    } 
} 

這點有點紅鯡魚的,但除去Microsoft.CSharp DLL實際上允許返回擴展方法成功地解決。然後我發現刪除了Microsoft.CSharp dll解決了我的問題,這一切都工作正常,mockSheet對象具有其所有屬性,並且能夠在「無法在空引用上執行運行時綁定」錯誤的情況下成功執行。

哦,對任何人嘲諷互操作類型的尖端,要格外注意設置此:

enter image description here

+0

這個答案是驚人的,哇,你是一個鐵桿編碼器的傢伙。你怎麼確定這個任何額外的線索? – 2012-03-23 03:07:22

+1

YEHA我問你! – 2012-03-23 03:07:39

+1

不,這是你所有的......我只是把(C)尖銳的東西扔在你的頭上,直到你想出來 – 2012-03-23 03:32:18

1

這是一個野生猜測,但辦公室互操作陣列是基於1的,而不是0爲主。我沒有看過它,但這可能會在元數據中定義。試試這個:

for (int i = 0; i < numSheets; i++) 
{ 
    listOfSheets.Add(Sheet); 
    listOfSheets[i].Name = MockSheetName + (i + 1); 
    `mockSheets[i + 1].Returns(listOfSheets[i]);` 
} 
+1

+1謝謝Jake。這不是修復,但你可以給任何建議是不勝感激。請參閱編輯,我的打印屏幕當今工程:) – 2012-03-22 20:30:17

1

聽起來像下面可能是原因:

VSTO具體項目進行測試(加載項/ taskpane /等)
VSTO版本:VSTO 3.0 SP1
.NET版本:.NET 3.5 SP1

VS 2010的p測試項目roject以上
默認爲
.NET版本:.NET 4.0

這之後創建的時候來模仿的對象作爲測試項目期待能夠使用MS.Csharp(我猜的)一個引用問題並可能有其他參考。

所以異常是不是真的指的在所有模擬對象返回空值,但不能夠加載.NET 4.0的CSHARP庫而造成的空結合例外。

因此當你發現瞭解決方法是刪除.NET 4.0 Csharp的參考。雖然將項目設置爲在.net 3.5下運行,但可能沒有必要。不確定你是否必須測試是否有其他問題發生。但是如果可以的話,我想最好在.net 4上保存測試項目。除非有人能指出這是不是最佳做法。

+0

關於沒有嵌入類型的好處(.net 4的特性) – 2012-03-23 03:40:39

1

這是一個提醒我,每次我寫Nsubstitute Excel的單元測試。我曾多次與這個錯誤作鬥爭。

您將得到錯誤:無法執行運行時對空引用

When you have referened the .Net Excel Object Library, you MUST reference the COM Microsoft Excel 14.0 Object Library. Once the COM interop Excel DLL is referenced, click F4 to see the DLLs Properties, remember to set the COM Interop NOT to Embed Interop Types.

.Excel下面結合是一個工作項目文件:

<ItemGroup> 
    <Reference Include="Microsoft.Office.Interop.Excel.Extensions"> 
     <HintPath>..\..\Refs\Microsoft.Office.Interop.Excel.Extensions.dll</HintPath> 
    </Reference> 
    <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> 
    <Reference Include="NSubstitute"> 
     <HintPath>..\..\Refs\NSubstitute.dll</HintPath> 
    </Reference> 
    <Reference Include="System" /> 
    <Reference Include="System.Core"> 
     <RequiredTargetFramework>3.5</RequiredTargetFramework> 
    </Reference> 
    <Reference Include="System.Data" /> 
    <Reference Include="System.Data.DataSetExtensions" /> 
    <Reference Include="System.Runtime.Serialization" /> 
    <Reference Include="System.Xml" /> 
    <Reference Include="System.Xml.Linq" /> 
    <Reference Include="UIAutomationProvider" /> 
    <Reference Include="VSTOContrib.Core, Version=0.9.0.52, Culture=neutral, processorArchitecture=MSIL" /> 
    <Reference Include="WindowsBase" /> 
    <Reference Include="WindowsFormsIntegration" /> 
    </ItemGroup> 
    <ItemGroup> 
    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> 
     <Visible>False</Visible> 
    </CodeAnalysisDependentAssemblyPaths> 
    </ItemGroup> 
    <ItemGroup> 
    <Compile Include="MockFactory.cs" /> 
    <Compile Include="Properties\AssemblyInfo.cs" /> 
    <Compile Include="UnitTests.cs" /> 
    </ItemGroup> 
    <ItemGroup> 
     <COMReference Include="Microsoft.Office.Core"> 
     <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid> 
     <VersionMajor>2</VersionMajor> 
     <VersionMinor>4</VersionMinor> 
     <Lcid>0</Lcid> 
     <WrapperTool>primary</WrapperTool> 
     <Isolated>False</Isolated> 
    </COMReference> 
    <COMReference Include="Microsoft.Office.Interop.Excel"> 
     <Guid>{00020813-0000-0000-C000-000000000046}</Guid> 
     <VersionMajor>1</VersionMajor> 
     <VersionMinor>6</VersionMinor> 
     <Lcid>0</Lcid> 
     <WrapperTool>primary</WrapperTool> 
     <Isolated>False</Isolated> 
    </COMReference> 
    </ItemGroup> 

罪犯是這個.Net互操作參考(需要是COM參考):

<Reference Include="Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL"> 
    <EmbedInteropTypes>True</EmbedInteropTypes> 
</Reference> 
相關問題