使用Moq

时间:2016-04-22 17:25:24

标签: c# unit-testing moq prism eventaggregator

使用以下代码订阅获取IEnumerable集合数据时,

获取不支持异常。无法订阅已发布的Collection对象。

Mock<IEventAggregator> _mockEventAgg = new Mock<IEventAggregator>();
_mockEventAgg.Setup(x => x.GetEvent<ShowScreenEvent>().Publish(new ObservableCollection<Customer>()
              { 
                 // Customer properties or details     
              }));


_mockEventAgg.Setup(m => m.GetEvent<ShowScreenEvent>().Subscribe(It.IsAny<Action<IEnumerable<Customer>>>()))
             .Callback<IEnumerable<Customer>>(customers => SelectedCustomerData = customers);

例外:

  

Moq.dll中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理

     

其他信息:非虚拟(VB中可覆盖)成员的设置无效:m =&gt; m.GetEvent()。订阅(It.IsAny())

2 个答案:

答案 0 :(得分:0)

您获得的例外情况说Moq未能设置方法,因为它是非虚拟的。 Moq不支持未标记为#!/bin/bash # Usage: # ./update_git_repos.sh [parent_directory] # example usage: # ./update_git_repos.sh C:/GitProjects/ [MAKE SURE YOU USE / SLASHES] updateRepo() { local dir="$1" local original_dir="$2" cd $dir # switch to the git repo repo_url=$(git config --get remote.origin.url) echo "****************************************************************************" echo "Updating Repo: $dir with url: $repo_url" echo "Starting update in $PWD" main_branch="master" if [ "$repo_url" == "git@someserver:repo/repo.git" ]; then # if you have a repo where the primary branch isnt master $main_branch="trunk" fi # update the repo, then stash any local changes echo -e "\ncalling: git fetch --all && git stash" (git fetch --all && git stash) current_branch=$(git rev-parse --abbrev-ref HEAD) # switch to master/trunk branch and rebase it, then switch back to original branch if [ $current_branch != $main_branch ]; then echo -e "\ncalling: git checkout $main_branch && git rebase && git checkout $current_branch" (git checkout $main_branch && git rebase && git checkout $current_branch) fi # rebase the original branch and then stash pop back to original state echo -e "\ncalling: git rebase && git stash pop on branch: $current_branch" (git rebase && git stash pop ) #switch back to the starting directory cd $original_dir echo "" } directory_to_update=${1} if [ -z "$directory_to_update" ] ; then echo "no directory passed in, using current directory" directory_to_update=$PWD fi echo "Updating git repo's in directory: $directory_to_update" count=0 for dir in $(find $directory_to_update -maxdepth 4 -type d -name .git | xargs -n 1 dirname); do updateRepo $dir $directory_to_update #& #uncomment to make it run in multiple threads, meh ((count+=1)) done echo "$count local git repos have been updated!" 的具体类型的模拟方法; see here了解更多信息。

在您的情况下,您试图模仿virtual,您说它是在其基类PubSubEvent<IEnumerable<Customer>>上定义的。实际上,它是非虚拟的:

ShowScreenEvent.Subscribe(...)

答案 1 :(得分:0)

您需要向设置中添加一个参数,以便模拟正确的订阅方法。 在棱镜中,只有带有有效载荷的事件的一种订阅方法被标记为虚拟

它看起来像这样

Eventaggregator.Setup(c => c.GetEvent<YourEvent>()
                             .Subscribe(It.IsAny<Action<string>>(),
                                        It.IsAny<ThreadOption>(), 
                                        It.IsAny<bool>(),
                                        It.IsAny<Predicate<string>>()))
                .Returns(YourHandleFunc);