- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 10283
Copy if newer doesn't work as I (or probably anyone else) would expect.
For example create new WPF project (CopyIfNewerHell):
Then, in solution add new Class Library:
Now, create an XML, like:
<?xml version="1.0" encoding="UTF-8"?> <rootNode> <childNode> Child </childNode> </rootNode>
Include that XML in the class library. On the end your solution should like something like:
Set properties of your XML:
Build action = Content, Copy to Output Directory = Copy if newer:
After that, add class library to CopyIfNewerHell project, so your references shoud look like:
Now you can hit F5. If you check bin folder, in my case it is here:
\Documents\Visual Studio 2012\Projects\CopyIfNewerHell\CopyIfNewerHell\bin\Debug\CopyIfNewerHell.xml
You will see CopyIfNewerHell.xml file. Open it in, for example, Notepad, and you will see:
<?xml version="1.0" encoding="UTF-8"?> <rootNode> <childNode> Child </childNode> </rootNode>
Now in Visual studio, open your xml, and change it, like:
<?xml version="1.0" encoding="UTF-8"?> <rootNode> <childNode> ChildNode </childNode> </rootNode>
Hit F5, and again if you open \Documents\Visual Studio 2012\Projects\CopyIfNewerHell\CopyIfNewerHell\bin\Debug\CopyIfNewerHell.xml in notepad, you will see that file was not changed. File CopyIfNewerHell.xml will be changed only if you change any property of that file. Also, if you change property Build action instead of Content to Embedded Resource then XML will be copied everytime when you change content of your file...
Solution I've found here.
---
Here I explained another possible solution.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 4998
Yield we will use when we want to return a collection (i.e. IEnumerable).
For example small console application:
namespace Yield { using System; using System.Collections.Generic; class Program { static void Main(string[] args) { IEnumerable<string> aaa; foreach (var a in YieldTest()) { Console.WriteLine(a); } Console.ReadKey(); } private static IEnumerable<string> YieldTest() { string[] myTestsStrings = new[] { "One", "Two" }; foreach (var myTest in myTestsStrings) { yield return myTest; } } } }
As a result we will see something like:
One
Two
Note line:
yield return myTest;
If we remove "yield" from that line, we will receive the error:
Cannot implicitly convert type 'string' to 'System.Collections.Generic.IEnumerable<string>'
Our YieldTest method can look like:
private static IEnumerable<string> YieldTest() { string[] myTestsStrings = new[] { "One", "Two" }; foreach (var myTest in myTestsStrings) { return new[] { myTest }; } return null; }
Notice that instead of:
yield return myTest;
Now we have:
return new[] { myTest };
Which will give as result something like:
One
Just one record.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 5353
I had task to check if XML nodes are in correct order. After some research basically all I needed was this line:
var nodes = myXml.Elements().ToList();
So, code would look something like this:
var nodes = myXml.Elements().ToList(); var expectedOrder = new[] { "first", "second", "third", "fourth" }.ToList(); var actualOrder = nodes.Select(node => node.Name.LocalName).ToList(); Assert.AreEqual(string.Join(",", expectedOrder), string.Join(",", actualOrder));
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 5770
Mocks are "fake" objects, that means that with mock you can create object which will have fake values.
For example, lets declare one interface without implentation, something like:
public interface ICalculator { int Summation(int a, int b); }
Now, our test method will look like something like this:
public void SetupMoqExampleTestMethod() { var mock = new Mock<ICalculator>(); mock.Setup(m => m.Summation(It.IsAny<int>(), It.IsAny<int>())).Returns(10); Assert.AreEqual(mock.Object.Summation(1, 1), 10); }
Notice the line:
mock.Setup(m => m.Summation(It.IsAny<int>(), It.IsAny<int>())).Returns(10);
With that line we said that which ever parameter we send to the "Summation" method, result will be always 10.
That is why our test will not fail.
Example you can download from here.