unit testing - c# XXXXX is inaccessible due to its protection level -


i having problem "xxxx inaccessible due protection level". have read lot of manuals how test classes internal access. have inserted

[assembly: system.runtime.compilerservices.internalsvisibletoattribute("unittests, publickey=mykey")] 

in projecta class want test. , in testproject have added reference projecta. both have same *.snk. keeps throwing error.

some suggestions?

without seeing specific class, it's hard precise, ensure following:

  • your class either internal or public
  • your method internal
  • the internalsvisibleto property contains full namespace of test project.

i.e. if test project myproject.unittests, need have in internalsvisibleto, e.g.:

[assembly: system.runtime.compilerservices.internalsvisibletoattribute("myproject.unittests")] 

i've never used publickey part of internalsvisibleto, can't comment on that, try removing it. works without me.

[assembly: system.runtime.compilerservices.internalsvisibletoattribute("myproject.unittests")] 

public key value

assuming fine, , it's still not working, guess it's publickey value entering. @ quick web search, appears don't enter key name, key value. that, follow this set of instructions value. i'll quote instructions here posterity.

the internalsvisibleto attribute can used allow assembly (such unit test assembly) see internal members of assembly. prefer method either putting unit tests in primary assembly or using vsts feature of autogenerating accessors private members. (those real fun when go refactor project…)

now of info find refers earlier beta usage. attribute used take in publickeytoken takes in publickey.

to use internalsvisibleto attribute:

  1. run sn.exe -p mystrongnamekey.snk mystrongnamekey.publickey

this extract public key file .publickey extension. (i hate using .pub extension because seen microsoft publisher file…)

  1. run sn.exe -tp mystrongnamekey.publickey

this display public key you. copy key.

  1. create attribute such:

    [assembly: internalsvisibleto( "myproject.unittests, publickey=paste-your-public-key-here" )]

  2. note public key displayed 5 lines or so. remove line breaks public key , paste attribute.

  3. you done! assembly myproject.unittests can see internal members of assembly.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -